Before getting started ๐
This article assumes a basic Expo project exists. For setup, see creating an Expo project on
Windows or
Mac .
Expo Router is a file-based router for React Native and web applications.
It manages navigation between screens so users can move seamlessly across the UI with the same components on Android, iOS, and the web.
Adding a file to the
App directory automatically creates a navigation route.
We will explore its page and dynamic routing features.
A file in the app directory automatically becomes a route.
For example, these files create the following paths.
๐ฆ app
โฃ ๐ index.tsx -> '/'
โฃ ๐ home.tsx -> '/home'
โฃ ๐ setting
โ โ ๐ index.tsx -> '/setting'
Here,
index.tsx defines the root route and
home.tsx defines
/home.
Use the routes as follows.
/app/index.tsx import { router } from 'expo-router' ;
import { Button } from 'react-native' ;
export default function Index () {
return (
<>
< Button
title = "Go to Home"
onPress ={ () => router. push ( '/home' ) }
/>
< Button
title = "Go to Setting"
onPress ={ () => router. push ( '/setting' ) }
/>
</>
);
}
2. Configuring layouts ๐
A directory's layout file defines shared UI such as headers and tab bars that persists between routes.
A new project's app directory includes a root layout, app/_layout, defining the layout for all routes.
Use a layout as follows.
๐ฆapp
โฃ ๐ _layout.tsx -> ๋ฃจํธ ๋ ์ด์์
โฃ ๐ index.tsx
โฃ ๐ home.tsx
โฃ ๐ setting
โ โ ๐ index.tsx -> /setting์ ์ ์ฉ๋๋ ๋ ์ด์์
Route groups collect routes that should share a layout.
Use a parenthesized folder name,
(๊ทธ๋ฃน๋ช
), meaning group name.
Groups created this way
do not affect route URLs
.
๐ฆapp
โฃ ๐ _layout.tsx
โฃ ๐ index.tsx
โฃ ๐ home.tsx
โฃ ๐ setting
โ โ ๐ index.tsx
โ ๐ (group) -> ๊ทธ๋ฃน ์์ฑ
โ โ ๐ _layout.tsx
โฃ โ ๐ sitemap.tsx -> '/sitemap'
โ โ ๐ about.tsx -> '/about'
Dynamic routing uses parameters as part of a path without affecting its other segments.
Use square brackets,
[], as follows.
๐ฆapp
โฃ ๐ _layout.tsx
โฃ ๐ index.tsx
โฃ ๐ home.tsx
โฃ ๐ user
โ โ ๐ [id].tsx -> '/user/{id}'
The
user/[id].tsx file defines
/user/{id}.
Use this route as follows.
/app/index.tsx import { router } from 'expo-router' ;
import { Button } from 'react-native' ;
export default function Index () {
return (
<>
< Button
title = "Go to User"
onPress ={ () => router. push ( '/user/1' ) }
/>
</>
);
}
id is the dynamic parameter, available inside user/[id].tsx as id.
/app/user/[id].tsx import { useLocalSearchParams } from 'expo-router' ;
import { Text, View } from 'react-native' ;
export default function User () {
const { id } = useLocalSearchParams ();
return (
< View >
< Text > { id } </ Text >
</ View >
);
}
Can a folder itself be a dynamic route? ๐
What we want:
๐ฆapp
โฃ ๐ _layout.tsx
โฃ ๐ index.tsx
โฃ ๐ home.tsx
โฃ ๐ [id]
โฃ โฃ ๐ mypage.tsx -> '/{id}/mypage'
โ โ ๐ edit.tsx -> '/{id}/edit'
Yes, dynamic routing also applies to folders.
Use
[] in the folder name.
/app/[id]/mypage.tsx import { useLocalSearchParams } from 'expo-router' ;
import { Text, View } from 'react-native' ;
export default function MyPage () {
const { id } = useLocalSearchParams <{ id : string }>();
return (
< View >
< Text > { id } </ Text >
</ View >
);
}
Expo's routing resembles Next.js App Router while offering a range of features.
It manages screen navigation so the same components can support smooth transitions across Android, iOS, and the web.
Platform-specific routing: