React-Router : What is the purpose of IndexRoute?

Nick Pineda picture Nick Pineda · Sep 22, 2015 · Viewed 66.9k times · Source

I don't understand what the purpose of using an IndexRoute and IndexLink. It seems that in any case the code below would of selected the Home component first unless the About path was activated.

<Route path="/" component={App}>
  <IndexRoute component={Home}/>
  <Route path="about" component={About}/>
</Route>

vs

<Route path="/" component={App}>
  <Route path="home" component={Home}/>
  <Route path="about" component={About}/>
</Route>

What's the advantage/purpose here of the first case?

Answer

Michelle Tilley picture Michelle Tilley · Sep 22, 2015

In the top example, going to / would render App with Home passed as a child. In the bottom example, going to / would render App with neither Home nor About being rendered, since neither of their paths match.

For older versions of React Router, more information is available on associated version's Index Routes and Index Links page. Starting in version 4.0, React Router no longer uses the IndexRoute abstraction to achieve the same goal.