--- name: expo-router description: Patterns for Expo Router including Stack configuration, native tabs, and file-based routing best practices. Apply when working with navigation, routing, or screen configuration. allowed-tools: Read, Edit, Write, Grep, Glob --- # Expo Router Patterns ## Stack Navigator Configuration ### Root Layout Setup Use `screenOptions` on the Stack component to set defaults for all screens. Do NOT explicitly list every screen - routes are auto-discovered from the file structure. ```tsx // app/_layout.tsx import { Stack } from 'expo-router'; export default function RootLayout() { return ( ); } ``` ### Per-Screen Configuration Individual screens configure their own options using `` within the component file: ```tsx // app/lesson/[id].tsx import { Stack } from 'expo-router'; export default function LessonScreen() { return ( {/* Screen content */} ); } ``` ### Dynamic Header Configuration Use `useNavigation` with `setOptions` for dynamic header content like buttons: ```tsx import { useNavigation } from 'expo-router'; import { useLayoutEffect } from 'react'; export default function Screen() { const navigation = useNavigation(); useLayoutEffect(() => { navigation.setOptions({ headerRight: () => ( ), }); }, [navigation]); return {/* content */}; } ``` ## Native Tabs (`expo-router/unstable-native-tabs`) Native tabs provide platform-native tab bar with SF Symbols on iOS: ```tsx // app/(tabs)/_layout.tsx import { Icon, Label, NativeTabs } from 'expo-router/unstable-native-tabs'; export default function TabLayout() { return ( ); } ``` ## Key Principles 1. **File-based routing**: Routes are auto-discovered from the `app/` directory structure 2. **Minimal configuration**: Only configure what you need to override 3. **Screen-level options**: Screens configure their own headers/options using `` within the component 4. **Layout files**: `_layout.tsx` files define navigation structure for their directory 5. **Route groups**: Parentheses like `(tabs)` create route groups without affecting the URL path ## Common Mistakes to Avoid - Don't list every screen explicitly in Stack - they're auto-discovered - Don't use `screenOptions` for route-specific settings - use `` in the route file - Don't nest navigators deeply - use file-based routing for cleaner structure