React-router: A <Router> may have only one child element

Created on 7 Apr 2017  路  7Comments  路  Source: ReactTraining/react-router

How to change this to fit it.

  <BrowserRouter>
      <Route path="/" component={App} />
      <Route path="/01" component={Record} />
  </BrowserRouter>

Most helpful comment

<BrowserRouter>
  <div>
      <Route path="/" component={App} />
      <Route path="/01" component={Record} />
  </div>
</BrowserRouter>

The <div> could also be a <Switch> if you only want one <Route> to render.

All 7 comments

<BrowserRouter>
  <div>
      <Route path="/" component={App} />
      <Route path="/01" component={Record} />
  </div>
</BrowserRouter>

The <div> could also be a <Switch> if you only want one <Route> to render.

No,it will have App inside Record.

I'm not exactly sure what you mean.

<Route path="/"> matches all paths. You will need to use the exact prop if you want that route to only match when location.pathname === '/'.

OK,but what's the different of Switch and Div

A <Switch> only renders the first child <Route> that matches the current location. The <div> would just be a placeholder to deal with the fact that (currently) React components can only return a single element.

But who will write many for same route path?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

maier-stefan picture maier-stefan  路  3Comments

tomatau picture tomatau  路  3Comments

wzup picture wzup  路  3Comments

ackvf picture ackvf  路  3Comments

ArthurRougier picture ArthurRougier  路  3Comments