Search code examples
reactjsreact-routerreact-router-component

React-Router | How do I get the parent parameter values from child component? version: 1.0.0-rc1


Assuming I have a code like this:

   <Router history={history}>
     <Route path="/users/(:userId)" component={UserContainer}>
       <Route path="profile" component={UserProfileContainer} />
       <Route path="stats" component={UserStatsContainer}>
       <IndexRoute component={UserDashboard} />
     </Route>
   </Router>

In the UserContainer, I have something like this:

const {userId} = this.props.params;
return (
  <div>
    {this.props.children}
  </div>
);

How can I pass the userId to the child components (UserProfileContainer, UserStatsContainer, etc) defined in the router config?

Those components need to know the current userId as well.

Thanks.


Solution

  • u can get it just like u did in 'UserContainer' : 'const {userId} = this.props.params;'....... : )