Course contentsShow
← Web Development
Lesson 981 of 2,98217. URL and History ManagementFree lesson

Nested Routes

Structure hierarchical navigation with parent and child routes for complex layouts.

Nested Routes

You'll learn how to organize multi-level navigation by defining parent routes that contain child routes, enabling complex page layouts with shared components.

The Core Idea

Nested routes structure your application's navigation hierarchically, much like folders contain subfolders on your computer. A parent route can render a layout (like a dashboard shell with a sidebar), while child routes render specific content within that layout.

Think of it like a house: the parent route is the main structure (walls, roof, hallways), and child routes are the individual rooms inside. When you navigate to /dashboard/settings, the /dashboard parent route renders the common shell, while /settings renders inside it.

How It Works

Your route structure maps URL segments to components:

/dashboard           → Dashboard layout
/dashboard/profile   → Dashboard layout + Profile content
/dashboard/settings  → Dashboard layout + Settings content

The parent route (/dashboard) stays mounted and renders a container. Child routes (/profile, /settings) swap out the specific content area without re-rendering the entire parent layout.

Pattern in Practice

You define nested routes by matching URL patterns that build on each other:

const routes = [
  {
    path: '/dashboard',
    component: DashboardLayout,  // Parent with nav/header
    children: [
      { path: 'profile', component: ProfilePage },
      { path: 'settings', component: SettingsPage }
    ]
  }
];

When handling navigation, you check if a route segment matches a parent, then check its children for deeper matches. The parent route typically includes a "slot" where child content renders.

Why Use Nested Routes

This pattern avoids duplicating layout code across pages, maintains UI state in parent components (like keeping a sidebar open), and creates intuitive URLs that reflect your app's structure.

Key Takeaway: Nested routes let you build hierarchical navigation where parent routes provide shared layouts and child routes render specific content within them, mirroring your URL structure.