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

后端 未结 9 1274
无人及你
无人及你 2020-12-08 18:05

I have a router like below:


    
        
          


        
相关标签:
9条回答
  • 2020-12-08 18:45

    In App.js add the below code and try

    window.location.pathname
    
    0 讨论(0)
  • 2020-12-08 18:52

    You can get the current route using

    const currentRoute = this.props.routes[this.props.routes.length - 1];
    

    ...which gives you access to the props from the lowest-level active <Route ...> component.

    Given...

    <Route path="childpath" component={ChildComponent} />
    

    currentRoute.path returns 'childpath' and currentRoute.component returns function _class() { ... }.

    0 讨论(0)
  • 2020-12-08 18:53

    You could use the 'isActive' prop like so:

    const { router } = this.context;
    if (router.isActive('/login')) {
        router.push('/');
    }
    

    isActive will return a true or false.

    Tested with react-router 2.7

    0 讨论(0)
提交回复
热议问题