反应路由器 |如何从子组件获取父参数值?版本:1.0.0-rc1

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

假设我有这样的代码:

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

在 UserContainer 中,我有这样的东西:

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

如何将 userId 传递给路由器配置中定义的子组件(UserProfileContainer、UserStatsContainer 等)?

这些组件还需要知道当前的用户 ID。

谢谢。

你可以像在 'UserContainer' 中那样得到它:'const {userId} = this.props.params;'....... :)