Skip to main content

Lesson 3 of 10

Nested Routes & Layouts

Nested routes are the most powerful feature of React Router v6. They allow layout components to persist across page transitions (sidebar, header, footer) while only the page content swaps. The <Outlet> component is the key - it renders the matched child route.

30 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Layout Components with Outlet

Layout Components with Outlet
import { Outlet, NavLink } from 'react-router-dom';

// DashboardLayout - persists sidebar and header across all /dashboard/* routes
function DashboardLayout() {
  return (
    <div className="dashboard">
      <Sidebar />   {/* renders on every /dashboard/* page */}
      <Header />    {/* renders on every /dashboard/* page */}
      <main>
        <Outlet />  {/* ← child route renders HERE */}
      </main>
    </div>
  );
}

function Sidebar() {
  return (
    <aside>
      <NavLink to="/dashboard" end>Overview</NavLink> {/* 'end' = only active on exact match */}
      <NavLink to="/dashboard/analytics">Analytics</NavLink>
      <NavLink to="/dashboard/settings">Settings</NavLink>
    </aside>
  );
}

// Route config - nested structure maps to URL structure
function App() {
  return (
    <Routes>
      <Route path="/" element={<RootLayout />}>
        <Route index element={<HomePage />} />

        {/* DashboardLayout wraps all /dashboard/* routes */}
        <Route path="dashboard" element={<DashboardLayout />}>
          <Route index element={<DashboardOverview />} />    {/* /dashboard */}
          <Route path="analytics" element={<Analytics />} /> {/* /dashboard/analytics */}
          <Route path="settings" element={<Settings />} />   {/* /dashboard/settings */}

          {/* Nested settings with its own sub-layout */}
          <Route path="settings" element={<SettingsLayout />}>
            <Route path="profile" element={<ProfileSettings />} />
            <Route path="security" element={<SecuritySettings />} />
          </Route>
        </Route>

        <Route path="*" element={<NotFound />} />
      </Route>
    </Routes>
  );
}

// Passing context via Outlet context (share data from layout to child routes)
function DashboardLayout() {
  const { data: user } = useQuery(['user'], fetchCurrentUser);
  return (
    <>
      <Sidebar />
      <Outlet context={{ user }} /> {/* pass data down to child routes */}
    </>
  );
}

// In child route - consume outlet context
import { useOutletContext } from 'react-router-dom';
function DashboardOverview() {
  const { user } = useOutletContext<{ user: User }>();
  return <h1>Welcome, {user.name}</h1>;
}

Tip

Tip

Practice Nested Routes Layouts in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and flow for Nested Routes & Layouts

Practice Task

Note

Practice Task - (1) Write a working example of Nested Routes Layouts from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Quick Quiz

Common Mistake

Warning

A common mistake with Nested Routes Layouts is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready react code.

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done