expo-router
Navigation and routing with Expo Router: file-based and dynamic routes, Link previews and context menus, native Stack, modals and form sheets, NativeTabs, headers, toolbars and search bars.
Install
npx skills add https://github.com/expo/skills --skill expo-routerRoute Structure
File Conventions
- Routes belong in the
appdirectory - Use
[]for dynamic routes, e.g.[id].tsx - Routes can never be named
(foo).tsx- use(foo)/index.tsxinstead - Use
(group)routes to simplify the public URL structure - NEVER co-locate components, types, or utilities in the app directory - these should be in separate directories like
components/,utils/, etc. - The app directory should only contain route and
_layoutfiles; every file should export a default component - Ensure the app always has a route that matches "/" so the app is never blank
- ALWAYS use
_layout.tsxfiles to define stacks
Dynamic Routes
Use square brackets for dynamic segments:
Catch-All Routes
Use [...slug] for catch-all routes:
Query Parameters
Access query parameters with the useLocalSearchParams hook:
For dynamic routes, the parameter name matches the file name:
[id].tsx→useLocalSearchParams<{ id: string }>()[slug].tsx→useLocalSearchParams<{ slug: string }>()
Pathname
Access the current pathname with the usePathname hook:
Group Routes
Use parentheses for groups that don't affect the URL:
Groups are useful for:
- Organizing related routes
- Applying different layouts to route groups
- Keeping URLs clean
Stacks and Tabs Structure
When an app has tabs, the header and title should be set in a Stack that is nested INSIDE each tab. This allows tabs to have their own headers and distinct histories. The root layout should often not have a header.
- Set the 'headerShown' option to false on the tab layout
- Use (group) routes to simplify the public URL structure
- You may need to delete or refactor existing routes to fit this structure
Example structure:
Array Routes for Multiple Stacks
Use array routes '(index,settings)' to create multiple stacks. This is useful for tabs that need to share screens across stacks.
This requires a specialized layout with explicit anchor routes:
Complete App Structure Example
Layout Files
Every directory can have a _layout.tsx file that wraps all routes in that directory:
Route Settings
Export unstable_settings to configure route behavior:
initialRouteNamewas renamed toanchorin v4
Not Found Routes
Create a +not-found.tsx file to handle unmatched routes: