Layout Routes
Layout routes (also called “pathless routes”) are routes that provide shared UI structure for child routes without adding segments to the URL. They’re perfect for shared navigation, authentication layouts, and grouping routes with common UI.Basic Layout Routes
Use thelayout() helper to create a layout route:
/dashboard→ rendersapp-layout.tsxwithdashboard.tsxin the outlet/profile→ rendersapp-layout.tsxwithprofile.tsxin the outlet/settings→ rendersapp-layout.tsxwithsettings.tsxin the outlet
/app-layout URL is created.
Layout Component
Layout routes must render an<Outlet /> for child routes:
Multiple Layout Routes
Create different layouts for different sections of your app:Nested Layout Routes
Layout routes can be nested for hierarchical UI structures:/account/profile renders:
app-layout.tsx- →
account/layout.tsx(in first outlet) - → →
account/profile.tsx(in second outlet)
File-Based Layout Routes
When usingflatRoutes(), prefix route names with _ to create layout routes:
_ indicates a pathless layout route.
Layout with Shared Data
Load shared data in layout routes:useMatches():
Authentication Layout
Protect routes with an auth layout:Layout with Context
Pass data to child routes via outlet context:Conditional Layouts
Apply different layouts based on conditions:Pathless Routes with Paths
Combine layout routes with path-based parent routes:/account/login→ account.tsx > public-layout.tsx > login.tsx/account/profile→ account.tsx > private-layout.tsx > profile.tsx
Layout Error Boundaries
Handle errors in layout routes:Layout Actions
Handle form submissions in layouts:Common Patterns
Marketing + App Split
Role-Based Layouts
Multi-Step Forms
Best Practices
- Shared UI only: Use layouts for shared UI elements (nav, header, footer)
- Minimal data loading: Only load data needed by the layout itself
- Clear naming: Name layout files descriptively (e.g.,
app-layout.tsx,auth-layout.tsx) - Error boundaries: Add error boundaries to layouts for better error handling
- Avoid deep nesting: Keep layout nesting to 2-3 levels maximum
- Context over props: Use outlet context for passing data to all children