ReactEasy

What is nested routing?

Nested routing means routes are defined inside other routes, so URL segments map to nested UI: a parent route renders a shared layout and an <Outlet />, and the matching child route renders inside that outlet. For /dashboard/settings, the dashboard layout stays and only the settings content changes.

How it works in React Router

  • Define children on a route object (or nest <Route> elements).
  • The parent's component renders <Outlet /> where children appear.
  • Index routes (index: true) render at the parent's exact path, such as /dashboard.
  • Layout routes without a path group children under a shared UI without adding a URL segment.
  • Each nested route can have its own loader, action, errorBoundary and middleware; loaders for all matched routes run in parallel.

Benefits

  • Shared layouts (sidebars, tabs, headers) are written once and stay mounted, keeping their state.
  • Only the changed part of the page re-renders on navigation.
  • Errors are isolated: a child's error boundary renders in its outlet while the parent layout keeps working.
  • Data loading in parallel avoids waterfalls.

Outlet context

<Outlet context={value} /> passes data to children, read with useOutletContext().

Next.js equivalent

Nested folders with layout.tsx files; children plays the role of <Outlet />.

Code example

tsx
1import { createBrowserRouter, NavLink, Outlet } from "react-router";
2
3function DashboardLayout() {
4 return (
5 <div className="flex">
6 <aside>
7 <NavLink to="/dashboard" end>Overview</NavLink>
8 <NavLink to="/dashboard/settings">Settings</NavLink>
9 <NavLink to="/dashboard/billing">Billing</NavLink>
10 </aside>
11 <main>
12 <Outlet /> {/* the matching child renders here */}
13 </main>
14 </div>
15 );
16}
17
18export const router = createBrowserRouter([
19 {
20 path: "/dashboard",
21 Component: DashboardLayout,
22 children: [
23 { index: true, Component: () => <h1>Overview</h1> }, // /dashboard
24 { path: "settings", Component: () => <h1>Settings</h1> }, // /dashboard/settings
25 {
26 path: "billing",
27 loader: () => fetch("/api/invoices").then((r) => r.json()), // runs in parallel with parent loaders
28 Component: () => <h1>Billing</h1>,
29 ErrorBoundary: () => <p>Billing failed to load</p>, // isolated error UI
30 },
31 ],
32 },
33]);

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the React set.

Open in study app