Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested routes in react-router

I'm setting up some nested routes within React-Router (v0.11.6 is what I'm working against) but whenever I try and access one of the nested routes it triggers the parent route.

My routes look like this:

<Route handler={App}>     <Route name="home" path="/" handler={availableRoutes.Splash} />     <DefaultRoute handler={availableRoutes.Splash} />      <Route name="dashboard" handler={availableRoutes.Dashboard}>         <Route name="dashboard-child" handler={availableRoutes.DashboardChild} />    </Route>      <NotFoundRoute handler={NotFound} /> </Route> 

If I collapse the routes up so it looks like:

<Route handler={App}>     <Route name="home" path="/" handler={availableRoutes.Splash} />     <DefaultRoute handler={availableRoutes.Splash} />      <Route name="dashboard" handler={availableRoutes.Dashboard} />     <Route name="dashboard-child" path="/dashboard/dashboard-child" handler={availableRoutes.DashboardChild} />      <NotFoundRoute handler={NotFound} /> </Route> 

It works fine. The reason I was nesting was because I will have multiple children under the "dashboard" and wanted them all prefixed with dashboard in the URL.

like image 854
Aaron Powell Avatar asked Dec 23 '14 01:12

Aaron Powell


People also ask

Can you have nested routes react Router?

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

What are nested routes react Router?

Nested Routes are a powerful feature. While most people think React Router only routes a user from page to page, it also allows one to exchange specific fragments of the view based on the current route.

How do I use nested routes in react Router?

Now, the last thing you need to do is tell React Router where in the parent Route ( Messages ) should it render the child Route ( Chats ). To do this, you use React Router's Outlet component. If the app's location matches the nested Route 's path , this Outlet component will render the Route 's element .


2 Answers

The configuration isn't about the routing (despite the name) but more about the layouts driven by paths.

So, with this configuration:

<Route name="dashboard" handler={availableRoutes.Dashboard}>   <Route name="dashboard-child" handler={availableRoutes.DashboardChild} /> </Route> 

It is saying that dashboard-child is to be embedded inside dashboard. How this works is that if dashboard has something like this:

<div><h1>Dashboard</h1><RouteHandler /></div> 

and dashboard-child has:

<h2>I'm a child of dashboard.</h2> 

Then for the path dashboard there is no embedded child due to no matching path, resulting in this:

<div><h1>Dashboard</h1></div> 

And for the path dashboard/dashboard-child the embedded child has a matching path, resulting in this:

<div><h1>Dashboard</h1><h2>I'm a child of dashboard.</h2></div> 
like image 66
bjfletcher Avatar answered Sep 25 '22 21:09

bjfletcher


Here's an update to @bjfletcher's answer for react-router 1.0.0. As noted in the upgrade guide:

RouteHandler is gone. Router now automatically populates this.props.children of your components based on the active route.

So instead of

<div><h1>Dashboard</h1><RouteHandler /></div>

you would use:

<div><h1>Dashboard</h1>{this.props.children}</div>

like image 39
Kevin Avatar answered Sep 22 '22 21:09

Kevin