Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i access the current hash location on react router 2?

I want to access the current location path (like /home) without the history keys for some comparison logic.

How can i access it from the hashHistory in react router 2.

Also, how can i access the previous path?

like image 763
Vivek Santhosh Avatar asked Mar 07 '16 12:03

Vivek Santhosh


1 Answers

You can obtain the current pathname from the location prop of your route component.

Access location from this.props.location of your Route component. If you'd like to get it deeper in the tree, you can use whatever conventions your app has for getting props from high down low. One option is to provide it on context yourself:

// v2.0.x const RouteComponent = React.createClass({
childContextTypes: { location: React.PropTypes.object },

getChildContext() { return { location: this.props.location } } })

Have a look here.

To get the current path, you can just use location.pathname.

like image 178
Alex Chirițescu Avatar answered Oct 15 '22 01:10

Alex Chirițescu