react-router V4 路由嵌套后,父路由如何向子路由传递特定参数

以下是嵌套代码

ReactDOM.render(
  <HashRouter>
    <App>
      <Route path='/login' component={Login} />
    </App>
  </HashRouter>,
  document.getElementById('root')

父路由App的代码

constructor(props) {
    super(props);
    this.state = {
      userName: '123'
    }
  }
render() {
    return <div className="App">
      {this.props.children}
    </div>
   }

如何向子路由login传递userName这个参数?

阅读 6k
3 个回答

暂时用context来向子路由组件传递父路由state中的数据
context用法

不知道你要如何实现。如果单纯是带数据过去的话可以这样

/login/:userName
新手上路,请多包涵

如果路由组件需要传递参数的话,Route组件不应该设置component,而是render,请看完react-router官方文档在决定是否要问问题。

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题