wxtry wxtry - 1 year ago 185
React JSX Question

How to get current route in react-router 2.0.0-rc5

I have a router like below:

<Router history={hashHistory}>
<Route path="/" component={App}>
<IndexRoute component={Index}/>
<Route path="login" component={Login}/>

Here's what I want to achieve :

  1. Redirect user to
    if not logged in

  2. If user tried to access
    when they are already logged in, redirect them to root

so now I'm trying to check user's state in
, then do something like:

if (!user.isLoggedIn) {
} else if(currentRoute == 'login') {

The problem here is I can't find the API to get current route.

I found this closed issue suggested using Router.ActiveState mixin and route handlers, but it looks like these two solutions are now deprecated.

Answer Source

After reading some more document, I found the solution:


I just need to access the injected property location of the instance of the component like:

var currentLocation = this.props.location.pathname
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download