React-router: how to get previous path using Link

4.6k views Asked by At

sorry for my poor English..my question is,when i use <Link/> to change current route into next route,how i can get previous path in next route's 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 />
        )
    }
})

thx ~ :-)

1

There are 1 answers

0
Guilherme Rodrigues On

Apparently there's no sanctioned way to do that.

As suggested in this issue, you can save 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 point "forward" after using the browser back button.