Skip to main content

Nested Routes

Nested routes allow you to compose complex UIs by nesting routes inside parent routes. Child routes render inside their parent’s <Outlet /> component, creating a hierarchical layout structure.

Basic Nested Routes

Define child routes using the children parameter:
This creates the following URL structure:
  • /dashboard → renders dashboard.tsx
  • /dashboard/overview → renders dashboard.tsx with overview.tsx in the outlet
  • /dashboard/analytics → renders dashboard.tsx with analytics.tsx in the outlet
  • /dashboard/settings → renders dashboard.tsx with settings.tsx in the outlet

Parent Route Component

The parent route must render an <Outlet /> where child routes appear:

Multi-Level Nesting

Routes can be nested to any depth:
URL structure:
  • /app/projects → app.tsx > projects.tsx > list.tsx
  • /app/projects/123 → app.tsx > projects.tsx > detail.tsx
  • /app/projects/123/edit → app.tsx > projects.tsx > detail.tsx > edit.tsx

Shared Layouts with Nested Routes

Nested routes are perfect for shared layouts:

Pathless Layout Routes

Use layout() to create routes that don’t add URL segments:
Both layouts share the /account URL prefix but provide different UI wrappers.

Data Loading in Nested Routes

Each route in the hierarchy can load its own data:
Both loaders run in parallel when navigating to /dashboard/analytics.

Accessing Parent Data

Child routes can access parent route data using useMatches():

Nested Navigation

Use relative paths in links within nested routes:

File-Based Nested Routes

When using flatRoutes(), use dot notation for nesting:
See File Conventions for more details.

Opt-Out of Parent Layout

In file-based routing, use trailing underscore to skip parent segments:
Or in manual config:

Error Boundaries in Nested Routes

Each route can export an error boundary:
Errors in child routes will bubble up to the nearest parent error boundary.

Best Practices

  1. Shallow hierarchies: Keep nesting to 3-4 levels maximum
  2. Logical grouping: Nest routes that share UI, not just URL structure
  3. Parallel loading: Leverage React Router’s automatic parallel data loading
  4. Layout reuse: Use pathless layouts to share UI without affecting URLs
  5. Independent routes: Don’t nest routes that don’t share layout just for URL structure