Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-router - creating nested routes with no Component nesting

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?

like image 952
Jithin Avatar asked Dec 17 '15 19:12

Jithin


People also ask

Can you have nested routes in react?

Routes can be nested inside one another, and their paths will nest too (child inheriting the parent).

How do you handle nested routes in react?

Nested Routes in React Router We will continue working on the User component, because this is the place where we want to have the nested routing via tabs. Therefore, we will instantiate a new set of Link components (which will be our unstyled tabs) which navigate a user to their profile and their account.

What are nested routes react router?

In my own words, a nested route is a region within a page layout that responds to route changes. For example, in a single-page application, when navigating from one URL to another, you do not need to render the entire page, but only those regions within the page that are dependent on that URL change.


1 Answers

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>
like image 115
taion Avatar answered Nov 15 '22 16:11

taion