marcelo.delta
marcelo.delta

Reputation: 3082

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>
  );
}

Upvotes: 0

Views: 63

Answers (1)

Anurag Awasthi
Anurag Awasthi

Reputation: 6233

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>

Upvotes: 3

Related Questions