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 thechildren parameter:
/dashboard→ rendersdashboard.tsx/dashboard/overview→ rendersdashboard.tsxwithoverview.tsxin the outlet/dashboard/analytics→ rendersdashboard.tsxwithanalytics.tsxin the outlet/dashboard/settings→ rendersdashboard.tsxwithsettings.tsxin 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:/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
Uselayout() to create routes that don’t add URL segments:
/account URL prefix but provide different UI wrappers.
Data Loading in Nested Routes
Each route in the hierarchy can load its own data:/dashboard/analytics.
Accessing Parent Data
Child routes can access parent route data usinguseMatches():
Nested Navigation
Use relative paths in links within nested routes:File-Based Nested Routes
When usingflatRoutes(), use dot notation for nesting:
Opt-Out of Parent Layout
In file-based routing, use trailing underscore to skip parent segments:Error Boundaries in Nested Routes
Each route can export an error boundary:Best Practices
- Shallow hierarchies: Keep nesting to 3-4 levels maximum
- Logical grouping: Nest routes that share UI, not just URL structure
- Parallel loading: Leverage React Router’s automatic parallel data loading
- Layout reuse: Use pathless layouts to share UI without affecting URLs
- Independent routes: Don’t nest routes that don’t share layout just for URL structure