Part 15 / Topic 2 of 9
Nested routes and layouts
A child <Route> nested inside a parent inherits the parent's path as a prefix, and renders through that parent's <Outlet/>. examples/02_nested_routes_and_layout.jsx's RestaurantLayout renders a heading and a nav exactly once, with an <Outlet/> where its children go:
// examples/02_nested_routes_and_layout.jsx
function RestaurantLayout() {
return (
<div>
<h1>Paradise Biryani - owner dashboard</h1>
<nav>{/* ... */}</nav>
<Outlet />
</div>
);
}
<Routes>
<Route path="/restaurant" element={<RestaurantLayout />}>
<Route index element={<Menu />} />
<Route path="orders" element={<Orders />} />
</Route>
</Routes>
index marks Menu as the child that renders at the parent's own URL (/restaurant, no extra segment) - the default child, the same role index.js plays in a folder. orders renders at /restaurant/orders, the parent's path plus its own. Visiting /restaurant really shows the dashboard heading and the menu; clicking the Orders link really keeps that exact same heading on screen the whole time and only swaps the <Outlet/>'s content to "3 orders waiting" (examples/02_nested_routes_and_layout.test.jsx) - the layout is not re-mounted, only the part that actually changed does.
A layout route is the same idea with one piece removed: a parent <Route> with no path of its own, used purely to group children under shared UI without adding a URL segment (<Route element={<MarketingLayout />}> wrapping children that each still get their own path, straight from the installed package's own routing doc). Section 7's RequireAuth is exactly this shape, used for a guard instead of a visual layout.
Done here? The practice set for Part 15 — including the solutions — is on the part page.