Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React Router V4 switch vs div? [duplicate]

I have seen various examples saying that when using React Router V4, you can wrap your <Route /> components in either a <Switch> component provided by React Router, or you can use any other element to contain your routes (such as a <div>).

What is the difference between these two approaches?

Examples:

<BrowserRouter>
  <div>
    <Route exact path="/login" component={LoginScreen} />
    <Route path="/loading" component={LoadingScreen} />
  </div>
</BrowserRouter>

VS

<BrowserRouter>
  <Switch>
    <Route exact path="/login" component={LoginScreen} />
    <Route path="/loading" component={LoadingScreen} />
  </Switch>
</BrowserRouter>
like image 728
saricden Avatar asked Nov 29 '17 02:11

saricden


1 Answers

<Switch/> components will only show the first matched child <Route/> for any given path. Other configs will show all matches.

like image 142
Arman Charan Avatar answered Oct 24 '22 13:10

Arman Charan