Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detect previous path in react router?

I am using react router. I want to detect the previous page (within the same app) from where I am coming from. I have the router in my context. But, I don't see any properties like "previous path" or history on the router object. How do I do it?

like image 914
vijayst Avatar asked Sep 02 '16 09:09

vijayst


1 Answers

You can pass down state using the <Link> component, in this case a pathname:

<Link to={{pathname: '/nextpath', state: { prevPath: location.pathname }}}>Example Link</Link> 

You can then access prevPath from this.props.location.state in the next component

like image 58
Sam Logan Avatar answered Oct 05 '22 13:10

Sam Logan