React-router: how to get the previous path using Link
Sorry for my bad english. My question is, when I use <Link/>
to change the current route to the next route, how can I get the previous path in the next route handler? here is some code
// route config
<Route handler={Route2} name="route2" path="route2" />
// eg: current location is #/route1?key=value
<Link to="route2">click to jump to route2</Link>
// route2
var Route2 = React.createClass({
componentDidMount: function() {
// how i can get previous path '#/route1?key=value'
},
render: function() {
return (
<div />
)
}
})
thanks ~ :-)
+3
source to share
1 answer
There seems to be no authorized way to do this.
As pointed out in this issue , you can keep the previous path in router.run
.
var prevPath = null;
router.run(function(Handler, state) {
React.render(<Handler prevPath={prevPath} />, document.body);
prevPath = state.path;
});
Beware, however, that this will indicate "forward" after using the back button.
+1
source to share