React Tutorial 0/120 lessons ~6 min read Lesson 49

    Nested Routing

    Nested Routing teaches parent and child routes, Outlet placeholders, multi-level nesting for product areas, and relative navigation patterns.

    Course progress0%
    Focus
    11 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Course: React — Routing & API Integration Lesson: 04

    Learning objectives

    Understand parent and child routes, use Outlet to render child content, and design nested route hierarchies for realistic product areas.

    Why nested routing?

    Enterprise applications share layout across related screens. An administration area may have a persistent sidebar and header while the main content changes between users, roles, billing, and audit logs. Nested routing models this hierarchy directly.

    text
    /admin
    |
    +-- /admin/users
    +-- /admin/roles
    +-- /admin/billing

    The parent route owns the shared layout. Child routes own the page-specific content.

    Parent layout with Outlet

    javascript
    import { Outlet, NavLink } from "react-router-dom";
    function AdminLayout() {
    return (
    <div className="admin-shell">
    <aside>
    <NavLink to="users">Users</NavLink>
    <NavLink to="roles">Roles</NavLink>
    <NavLink to="billing">Billing</NavLink>
    </aside>
    <section className="admin-content">
    <Outlet />
    </section>
    </div>
    );
    }

    Outlet is the placeholder where the matched child route renders. Without it, the parent layout can appear while the child page remains invisible.

    Define nested routes

    javascript
    <Routes>
    <Route path="/admin" element={<AdminLayout />}>
    <Route index element={<AdminOverview />} />
    <Route path="users" element={<UsersPage />} />
    <Route path="roles" element={<RolesPage />} />
    <Route path="billing" element={<BillingPage />} />
    </Route>
    </Routes>

    The index route is the default content at /admin. Relative child paths are appended to the parent path.

    Multi-level nesting

    A customer management area may have /workspace/customers/:customerId as a parent and nested tabs such as activity, invoices, and settings. The customer layout can load or display shared customer context and render each tab through its own Outlet.

    Keep nesting aligned with genuine shared layout or domain structure. Deep nesting without a clear benefit can make route ownership harder to understand.

    Within a parent route, relative links can keep navigation resilient if the parent path changes. Use absolute links when clarity or cross-section navigation is more important. Verify the final resolved URL in tests.

    Error and loading boundaries

    A nested route can have route-level error handling in data-router configurations, and feature components can render their own loading and failure states. Keep the user experience coherent: a child page failure should not unnecessarily destroy the entire application shell.

    Common mistakes

    • Missing Outlet.
    • Confusing index routes with a child path named index.
    • Using incorrect relative paths.
    • Making every component a nested route even when no shared layout exists.
    • Putting all data loading into a large parent when children have independent data needs.

    Best practices

    • Nest routes around shared layout and domain boundaries.
    • Keep the route hierarchy understandable.
    • Use index routes for default child content.
    • Test direct access to deeply nested URLs.
    • Keep navigation links consistent with the route tree.

    Interview questions & answers

    Interview preparation

    5 questions grouped by difficulty — expand any card for a model answer and follow-up probe.

    Beginner

    2
    1. 1What problem does nested routing solve?
      Beginner

      Model answer

      It models shared shells—sidebars, headers, workspace chrome—while child routes swap only the page-specific content beneath a stable parent layout.

    2. 2When would you avoid nesting routes?
      Beginner

      Model answer

      Avoid nesting when there is no shared layout or domain boundary—deep trees without a clear benefit make ownership harder to follow.

    Intermediate

    2
    1. 3What is the purpose of Outlet?
      Intermediate

      Model answer

      Outlet marks where the active child route renders inside the parent layout. Without it, the shell appears but child pages stay invisible.
    2. 4How would you model tabs for a customer detail page?
      Intermediate

      Model answer

      Use a customer layout parent with nested child routes for each tab (overview, activity, invoices), sharing customer context while each tab renders through Outlet.

    Advanced

    1
    1. 5How does an index route differ from a normal child route?
      Advanced

      Model answer

      An index route renders at the parent URL itself. A normal child adds a relative path segment such as users under the parent.

    Practice

    Build /workspace with a shared sidebar and nested Dashboard, Customers, Orders, and Settings pages. Add a nested customer detail layout with Overview and Activity tabs.

    Ready to mark this lesson complete?Track your journey across the entire course.