Layouts
Layouts allow you to share UI and navigation structure across multiple routes. They’re defined using_layout files and wrap all routes in their directory.
What Are Layouts?
A layout is a component that wraps child routes. It can:- Provide shared navigation structure (Stack, Tabs, Drawer)
- Share UI elements like headers or footers
- Manage state across child routes
- Control screen transitions
Creating a Layout
Create a_layout.tsx file in any directory:
app/_layout.tsx
app directory.
Layout Types
Stack Layout
Provides native stack navigation with headers:app/_layout.tsx
Tabs Layout
Provides bottom tab navigation:app/(tabs)/_layout.tsx
Drawer Layout
Provides side drawer navigation:app/_layout.tsx
Nested Layouts
Layouts can be nested to create complex navigation structures:Root Layout
app/_layout.tsx
Tab Layout
app/(tabs)/_layout.tsx
Nested Stack Layout
app/(tabs)/profile/_layout.tsx
Slot Component
Use<Slot /> to render child routes without a navigator:
app/_layout.tsx
Navigator.tsx:92-127):
Screen Configuration
Configure individual screens within layouts:app/_layout.tsx
Dynamic Screen Options
Set screen options from within the route:app/profile.tsx
Shared State
Share state across routes using React Context:app/_layout.tsx
app/settings.tsx
Layout Groups
Use route groups to apply different layouts without affecting URLs:app/(auth)/_layout.tsx
Initial Route Name
Set the initial route for a layout:app/(tabs)/_layout.tsx
navigation.test.ios.tsx:21-56):
Custom Navigator
Create a custom navigator withNavigator component:
app/(custom)/_layout.tsx
Navigator.tsx:37-90):
Layout with Context
Create reusable layout components:components/AppLayout.tsx
app/(app)/_layout.tsx
Best Practices
Keep Layouts Simple
Use Route Groups
Organize routes with different layouts:Configure Screen Options
Set defaults in layout, override in screens:Share Data Efficiently
Use Context for shared state, props for screen-specific data:Common Patterns
Auth Flow with Layouts
app/_layout.tsx
Modal Stack
app/_layout.tsx
Tabs with Nested Stacks
app/(tabs)/home/_layout.tsx
Next Steps
Route Groups
Organize routes without affecting URLs
Stack Navigation
Configure stack navigators
Tab Navigation
Create tab navigators
Drawer Navigation
Add drawer navigation