react-router - creating nested routes with no Component nesting

- 1 answer

Ad

I have a route config like this.

<Route path="group/:groupId" component={NonPropertyView}>
<Route path="group/:groupId/line/:lineId" component={NonPropertyView} />
<Route path="group/:groupId/line/:lineId/property/:propertyId" component={PropertyView} />

But can I do this ?

<Route path="group/:groupId" component={NonPropertyView}>
  <Route path="line/:lineId" component={NonPropertyView}>
    <Route path="property/:propertyId" component={PropertyView} />
  </Route>
</Route>

What I am looking for is an option to just render Component for leaf Route without rendering a parent route Component. Is this possible?

Ad

Answer

Ad

Yes - use <IndexRoute>s. For example, write the above as:

<Route path="group/:groupId">
  <IndexRoute component={NonPropertyView} />
  <Route path="line/:lineId">
    <IndexRoute component={NonPropertyView} />
    <Route path="property/:propertyId" component={PropertyView} />
  </Route>
</Route>
Ad
source: stackoverflow.com
Ad