Nested routes and layouts

Intermediate · 39 min

Sharing a persistent layout across routes with <Outlet>, index routes for the default child, and a worked dashboard example.

Before thisReact Router fundamentalsCompound components

PedroTech · 51 min · from 12:27

Nested routes and layouts

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by PedroTechReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • A route object can have `children`, and each child route renders inside its parent's `<Outlet>` rather than replacing the whole page, so shared layout only renders once.
  • `<Outlet>` is a placeholder component: wherever you put it inside a parent route's element, React Router inserts whichever child route currently matches.
  • An `index: true` child route is the default content shown at the parent's own path — `/dashboard` renders the parent plus its index child, with no extra URL segment needed.
  • Because the parent layout only renders once per matching parent path, state inside it (an open sidebar section, a scroll position) survives navigating between its children, unlike re-rendering the whole page from scratch.
  • Nesting is a direct parallel to component composition: a layout component that renders `{children}` and a parent route that renders `<Outlet>` are solving the same problem — 'render fixed chrome around something that varies' — at two different layers.
  • Relative paths inside a nested route (children of `/dashboard` written as `"settings"` rather than `"/dashboard/settings"`) are resolved against the parent's path automatically.

The problem: shared chrome, changing content

The previous lesson’s route table treated every page as independent: match a path, render an element, done. Real apps rarely work that way. A dashboard typically has a sidebar and a header that stay on screen across every page under /dashboard, with only the main content area actually changing as the user moves between /dashboard/overview and /dashboard/settings. If each of those were its own flat, top-level route, you’d either duplicate the sidebar markup in every single element, or wrap them all in a layout component manually and hope you remembered to do it consistently everywhere. Neither scales, and both throw away and re-create the sidebar’s DOM — and any state it holds, like which section is expanded — on every single navigation.

Nesting routes under a shared parent

React Router solves this with a children array on a route object, paired with a new component: <Outlet>.

import { createBrowserRouter, RouterProvider, Outlet, Link } from "react-router-dom";

const router = createBrowserRouter([
  {
    path: "/dashboard",
    element: <DashboardLayout />,
    children: [
      { index: true, element: <Overview /> },
      { path: "settings", element: <Settings /> },
      { path: "billing", element: <Billing /> },
    ],
  },
]);

DashboardLayout is the parent route’s element — the sidebar, header, and anything else that should persist across every dashboard page. It renders once per visit to anything under /dashboard, and it uses <Outlet> to say where the matching child should appear:

function DashboardLayout() {
  return (
    <div className="dashboard">
      <nav className="sidebar">
        <Link to="/dashboard">Overview</Link>
        <Link to="/dashboard/settings">Settings</Link>
        <Link to="/dashboard/billing">Billing</Link>
      </nav>
      <main>
        <Outlet />
      </main>
    </div>
  );
}

Navigate to /dashboard/settings, and React Router renders DashboardLayout, then slots <Settings /> into the <Outlet>. Navigate to /dashboard/billing next, and DashboardLayout itself is not re-mounted — only the content inside <Outlet> swaps from <Settings /> to <Billing />. The sidebar’s DOM, any scroll position inside it, and any component state it holds are left completely alone.

Index routes: the parent’s default page

Notice the first child above has index: true instead of a path. That’s an index route — it’s what renders inside <Outlet> when the URL matches the parent’s path exactly, with no further segment. Visiting /dashboard with no trailing path renders DashboardLayout with <Overview /> inside its <Outlet>; visiting /dashboard/settings renders <Settings /> instead. Exactly one child of a given parent can be an index route, and it’s the right place for whatever a section’s “default view” should be rather than requiring the user to type an extra segment to see anything at all.

Nesting more than one level deep

Nothing restricts this to a single level. A children array’s own entries can have further children, and each nested level needs its own <Outlet> in its own element:

{
  path: "settings",
  element: <SettingsLayout />,   // renders its own tabs + an Outlet
  children: [
    { index: true, element: <ProfileSettings /> },
    { path: "security", element: <SecuritySettings /> },
  ],
}

SettingsLayout might render a row of tabs plus its own <Outlet> for whichever settings sub-page is active — the same pattern, one layer deeper. This is worth connecting back to ordinary component composition: a React layout component that renders {props.children} around some fixed chrome, and a parent route that renders <Outlet> around some fixed chrome, are the exact same idea — “wrap something variable in something constant” — applied at the component layer versus the routing layer. If you’re comfortable building a <Card> that wraps arbitrary children, nested routes are the same instinct, just driven by the URL instead of by what a caller passes in JSX.

What the video covers

This lesson uses the nested-routing chapter of the same PedroTech React Router v7 tutorial the fundamentals lesson draws on — specifically its segment on nested routes, layouts, and route prefixing. The video demonstrates nesting through its file/config-based “framework mode” conventions rather than writing the children array and <Outlet> calls directly as shown above; the underlying concept — a persistent layout rendering an <Outlet> around a matched child — is identical in either form, which is why the video is still the right watch for seeing this working in a real, built-out app. The worked dashboard example, the index-route explanation, and the explicit parallel to component composition above are this lesson’s own material, written to show the declarative children/<Outlet> code directly rather than only through the video’s generated-route lens.

Where this fits

Nested routes get you shared layout, but so far every page’s data has been hard-coded or assumed to already exist. The next lesson covers React Router’s own way of loading that data before a route renders: data loading with router loaders.

Check yourself

  1. 01What does `<Outlet>` do inside a layout route's element?

    It marks the spot where React Router should render whichever child route currently matches the rest of the URL; without an `<Outlet>` in the parent's element, matched children have nowhere to render.

  2. 02What is an index route, and when does it render?

    An index route is a child route marked `index: true` with no `path` of its own, and it renders inside the parent's `<Outlet>` whenever the URL matches exactly the parent's path with no further segment — it's the parent's default content.

  3. 03In a dashboard with a persistent sidebar, why does nesting the routes under one parent perform better than giving each page its own completely separate route with a copy of the sidebar?

    With nesting, the sidebar lives in the parent route's element and renders once; only the `<Outlet>` content re-renders when you navigate between child routes, so the sidebar's own state (like which section is expanded) and its DOM are left untouched. Separate top-level routes would each re-mount their own copy of the sidebar from scratch on every navigation.

  4. 04If a parent route has `path: "dashboard"` and a child route has `path: "settings"`, what full URL does that child match?

    `/dashboard/settings` — a child route's path is resolved relative to its parent's matched path, so you write just the segment that's added, not the full accumulated path.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark