search

Home  >  Q&A  >  body text

javascript - react-router V4 After routing is nested, how does the parent route pass specific parameters to the child route?

The following is the nested code

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

Code of parent routing App

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

How to pass the userName parameter to the sub-route login?

曾经蜡笔没有小新曾经蜡笔没有小新2751 days ago576

reply all(3)I'll reply

  • phpcn_u1582

    phpcn_u15822017-05-18 10:50:15

    Temporarily use context to pass the data in the parent routing state to the child routing component
    Context usage

    reply
    0
  • 为情所困

    为情所困2017-05-18 10:50:15

    I don’t know how you want to achieve it. If you just bring data there, you can do this

    /login/:userName

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-18 10:50:15

    If the routing component needs to pass parameters, the Route component should not set component, but render. Please read the react-router official documentation before deciding whether to ask questions.

    reply
    0
  • Cancelreply