简体   繁体   中英

How to use Components on Different Routes in React?

How to use Components on Different Routes in React?

I've tried to accomplish it like this, but I get an error:

<Router>
    <App>
        <Route exact path='/registro' component={Registro}/>
        <Route exact path='/registrar' component={Registrar}/>
        <Route exact path="/home" component={Home} />
    </App>
    <Route exact path="/login" component={Login} />
</Router>,

document.getElementById('root'));

App:

render() {
  const { children } = this.props;

  return (
    <div>
      {children}
    </div>
  );
}

Router should have only one child. So you could wrap your elements in a single div, like so:

<Router>
  <div>
    <App>
      <Route exact path='/registro' component={Registro}/>
      <Route exact path='/registrar' component={Registrar}/>
      <Route exact path="/home" component={Home} />
    </App>
    <Route exact path="/login" component={Login} />
  </div>
</Router>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM