问题
I have the browser cache as the central single source of truth.
When I want to change the app state, I just change the broser cache and call setState to trigger a render().
Everything works fine unless the user changes the adress manually (refreshes the window, clicks on a bookmark, etc...), to fix that, I made the router's render method (<Router render={render}>
) save the url's params myUrl/:myVar1/:myVar2
in the browser cache too
My issue is that I don't want the router to ovveride what is in the cache unless it was called manually (eg. by pasting a url). Is there a way to recognize the origin of the url change (if it comes from a history.push or another action)?
回答1:
I am not entirely sure if I can help. But I'll share what I do in case its what you're looking for.
This is what I do to keep a hold of the last pathname so that I can have a back button
import React, { PureComponent } from "react";
import { withRouter } from "react-router-dom";
class Navigation extends PureComponent {
jump = path => {
this.props.history.push(this.props.history.location.pathname);
this.props.history.replace(path);
};
render() {
}
}
export default withRouter(Navigation);
The with router package allows us to inject the window history object into any component
Let me know if any more queries
Daniel
来源:https://stackoverflow.com/questions/53210414/how-to-recognize-if-the-route-was-initiated-through-a-history-push-or-a-manual-a