Expo Router navigation
You can map route files and layouts to guarded screens, giving the app separate signed-out and signed-in navigation trees.
Learning outcomes
- Map Expo Router files and folders to screens and URLs.
- Choose Stack, Tabs and Slot navigators through nested layout routes.
- Handle dynamic route parameters, links, typed routes and modal navigation.
- Protect signed-in screens using authentication state and
Stack.Protected. - Account for asynchronous authentication startup and deep links.
The Expo app needs separate screens for Log in, Restaurants, Orders, Cart, restaurant menus, item configuration and order details. A customer must be able to open a restaurant from a link, move between tabs and return to the previous screen without losing its state. A signed-out customer must never reach a protected restaurant screen, even when opening a deep link directly.
Files as routes
Expo Router uses file-based routing. Files and folders inside the Expo project's app/ directory define the available screens. You do not maintain a separate route table.
Inside this project, the route files live in app/app/, because the Expo project itself lives in the repository's app/ directory.
![The Expo project's app directory contains the root _layout.tsx and +not-found.tsx, an (auth) group with its layout, login and signup screens, a (tabs) group with its layout, restaurants index, orders and cart screens, plus restaurant/[id].tsx, item/[id].tsx and orders/[id].tsx.](/zomato/diagrams/w4-app-file-tree-light.png)
![The Expo project's app directory contains the root _layout.tsx and +not-found.tsx, an (auth) group with its layout, login and signup screens, a (tabs) group with its layout, restaurants index, orders and cart screens, plus restaurant/[id].tsx, item/[id].tsx and orders/[id].tsx.](/zomato/diagrams/w4-app-file-tree-dark.png)
The files have specific meanings:
app/_layout.tsxis the root layout. It wraps every screen and owns the root navigator and app-wide providers.app/+not-found.tsxhandles URLs that do not match a route.app/(auth)/contains the signed-out screens and their Stack layout.app/(tabs)/contains the three main signed-in screens and their Tabs layout.app/restaurant/[id].tsxdisplays a restaurant menu.app/item/[id].tsxdisplays the item configurator.app/orders/[id].tsxdisplays an order's details.
Here is a small route screen:
// app/(auth)/login.tsx
import { Text, View } from "react-native";
export default function LoginScreen() {
return (
<View>
<Text>Log in</Text>
</View>
);
}The default export becomes the screen for that route. Its visible content uses React Native core components, not HTML elements.
Four naming rules cover most of the route tree:
index.tsxrepresents the root route of its folder.[id].tsxrepresents a dynamic segment._layout.tsxdefines a layout around the routes in its folder.+not-found.tsxhandles unmatched routes.
For example, app/restaurant/[id].tsx matches /restaurant/42. The same file handles every restaurant ID, rather than requiring a separate file for each restaurant.
Layout routes and navigators
A layout route is a _layout.tsx file. It wraps the routes in its folder and decides which navigator manages them.
Expo Router provides three common navigators:
| Navigator | Purpose |
|---|---|
Stack | Pushes screens on top of each other and supports back navigation |
Tabs | Displays screens behind a bottom tab bar |
Slot | Renders a matched child without adding a navigator |
You can nest layouts. A root Stack can contain a group with another Stack, or a group with Tabs.
The Expo app uses a root Stack because some screens need to appear on top of the main tab bar. The auth group has its own Stack for Log in and Create account. The tabs group has its own Tabs navigator for Restaurants, Orders and Cart.
The auth layout declares the two screens it manages:
import { Stack } from "expo-router";
export default function AuthLayout() {
return (
<Stack>
<Stack.Screen name="login" options={{ title: "Log in" }} />
<Stack.Screen name="signup" options={{ title: "Create account" }} />
</Stack>
);
}The names correspond to files in the same folder. Screen options configure the native navigation interface, such as the header title.
The tabs layout follows the same idea:
import { Tabs } from "expo-router";
export default function TabsLayout() {
return (
<Tabs>
<Tabs.Screen name="index" options={{ title: "Restaurants" }} />
<Tabs.Screen name="orders" options={{ title: "Orders" }} />
<Tabs.Screen name="cart" options={{ title: "Cart" }} />
</Tabs>
);
}Each tab remains part of the navigation tree. Switching between tabs does not require you to build a separate screen-rendering system.
Route groups
The parentheses in (auth) and (tabs) identify route groups. A route group organizes screens under a shared layout without adding a URL segment.
For example:
| File | URL |
|---|---|
app/(auth)/login.tsx | /login |
app/(auth)/signup.tsx | /signup |
app/(tabs)/index.tsx | / |
app/(tabs)/orders.tsx | /orders |
app/(tabs)/cart.tsx | /cart |
The group name does not appear in the URL. You can therefore have separate layouts for signed-out and signed-in screens without exposing an /auth or /tabs segment to the user.
This separation also gives the root navigator a convenient way to protect both sets of screens as units.
Dynamic routes and parameters
Restaurant and order IDs are not known when you build the app. A dynamic route uses a bracketed filename to match all IDs.
The restaurant route is restaurant/[id].tsx. It can therefore represent /restaurant/1, /restaurant/42 or /restaurant/105.
Read the parameter with useLocalSearchParams:
import { useLocalSearchParams } from "expo-router";
import { Text } from "react-native";
export default function RestaurantScreen() {
const { id } = useLocalSearchParams<{ id: string }>();
return <Text>Restaurant {id}</Text>;
}The generic type describes the parameter. It does not convert the value into a number.
Route parameters are strings. If the Go API needs a numeric restaurant ID, convert and validate it before using it.
const restaurantId = Number(id);
if (!Number.isInteger(restaurantId) || restaurantId <= 0) {
return <Text>Invalid restaurant</Text>;
}The positive-integer check rejects malformed IDs and values such as zero or negative numbers. Do not trust a route parameter because it came from an internal link. A deep link can contain arbitrary input.
Links and navigation actions
A Link navigates when the user taps it. It can render ordinary link content or turn a native component into a navigation target with asChild.
import { Link } from "expo-router";
import { Pressable, Text } from "react-native";
<Link href="/restaurant/42" asChild>
<Pressable>
<Text>View restaurant</Text>
</Pressable>
</Link>With asChild, Expo Router passes the navigation behavior to its single child. This lets you make a card or button navigate while retaining native press behavior.
When navigation follows an action rather than a direct tap on a link, use the router object.
| Method | Behavior |
|---|---|
router.push(href) | Adds a screen to the navigation stack |
router.replace(href) | Replaces the current screen |
router.back() | Returns to the previous screen |
Consider a customer opening a restaurant from the Restaurants tab. A push places the menu screen above the tab screen. Back returns to the same tab and its previous state.
import { router } from "expo-router";
function openRestaurant(restaurantId: number) {
router.push({
pathname: "/restaurant/[id]",
params: { id: String(restaurantId) },
});
}For dynamic routes, pass an object with the pathname and parameters together. This avoids constructing URL strings manually and makes the relationship between the route pattern and its parameters explicit.
Use replace when the previous screen should not remain in the navigation history. For example, after a successful order, the checkout screen may no longer be appropriate to revisit. Use back when the user should return to the screen they came from.
Typed routes
A route can look correct at runtime but still contain a typo that TypeScript cannot detect when navigation is untyped. Expo Router can generate types from the files in your route tree.
Enable experiments.typedRoutes in app.config.ts:
import type { ExpoConfig } from "expo/config";
const config: ExpoConfig = {
name: "Zomato prep",
slug: "zomato-prep",
experiments: { typedRoutes: true },
};
export default config;Run the development server once to generate the route type definitions. TypeScript can then check route paths and required parameters in Link and router.push.
For example, a link to /restaurant/[id] must provide its id parameter. A typo in a route name becomes a type error instead of remaining hidden until someone taps the link.
Typed routes do not validate parameter values at runtime. A typed id can still contain an invalid number. Keep the runtime validation on the restaurant screen.
Screen options and modals
Each navigator's Screen component configures how a route appears. You can set options in its layout or inside the screen when its title depends on data available there.
For a restaurant screen, the title can use the ID from the route:
import { Stack } from "expo-router";
import { Text, View } from "react-native";
export default function RestaurantScreen() {
const restaurantId = 42;
return (
<View>
<Stack.Screen options={{ title: `Restaurant ${restaurantId}` }} />
<Text>Menu for restaurant {restaurantId}</Text>
</View>
);
}The item configurator needs a different presentation. It should appear over the restaurant menu and close back to it, rather than behaving like another tab.
Set presentation: "modal" on the corresponding screen in the root Stack:
<Stack.Screen
name="item/[id]"
options={{ presentation: "modal", title: "Customise item" }}
/>A modal is still a route. It can have a dynamic parameter, participate in navigation history and respond to the back action.
The item screen belongs in the root Stack rather than inside the tabs. This allows it to cover the tab bar while the menu remains underneath.
Protected routes
The customer app has two authentication states. Signed-out customers should reach Log in and Create account. Signed-in customers should reach Restaurants, Orders, Cart and the screens opened from them.
A protected route uses Stack.Protected to make screens available only when a boolean guard is true.
You could implement authentication redirects with effects, but that creates problems. A protected screen may render before the effect redirects the user. The unwanted screen may remain in navigation history. You would also need to repeat the authentication check across multiple screens.
A protected navigator puts the decision in one place:
<Stack>
<Stack.Protected guard={!isSignedIn}>
<Stack.Screen name="(auth)" />
</Stack.Protected>
<Stack.Protected guard={isSignedIn}>
<Stack.Screen name="(tabs)" />
</Stack.Protected>
</Stack>When isSignedIn changes, the guards change which screens are available.
This has several effects:
- A navigation attempt to a screen whose guard is false is refused, including navigation from a
Linkor a deep link. - When a guard becomes false, the screens it protected are removed from navigation history.
- When authentication changes, the navigator moves to the first available screen in the newly active group.
The real root Stack also needs to guard the restaurant menu, order detail and item configurator. Protecting only (tabs) would leave those direct routes outside the signed-in boundary.
Asynchronous authentication startup
The app does not know whether a customer is signed in immediately after startup. The access token and refresh token are stored in expo-secure-store, and reading them is asynchronous.
If you render the navigator with an assumed signed-out state, a returning customer can briefly see Log in before the stored tokens are read. The same problem can occur in the opposite direction if you assume signed in without checking storage.
Represent startup with an isLoading state. The root navigator waits until authentication is known.
const { isLoading, isSignedIn } = useAuth();
if (isLoading) {
return <ActivityIndicator size="large" />;
}
return (
<Stack>
<Stack.Protected guard={!isSignedIn}>
<Stack.Screen name="(auth)" />
</Stack.Protected>
<Stack.Protected guard={isSignedIn}>
<Stack.Screen name="(tabs)" />
</Stack.Protected>
</Stack>
);The loading state prevents the Stack from rendering while the token store is being read. Once the read completes, isSignedIn determines which group is available.
The component that calls useAuth() must be inside AuthProvider. React context flows from parent to child. A component cannot read the context value from a provider that it renders below itself.
The provider order in the root layout is also important. The QueryClient is created at module level so it does not get recreated on each render. The navigator sits inside the auth provider because it reads auth state.
const queryClient = new QueryClient();
export default function RootLayout() {
return (
<QueryClientProvider client={queryClient}>
<AuthProvider>
<RootNavigator />
</AuthProvider>
</QueryClientProvider>
);
}RootNavigator calls useAuth(), so it must remain a child of AuthProvider. The query provider stays outside the auth provider because later screens need access to the query client regardless of authentication state.
The navigation tree
The complete app combines the root Stack, two route groups and three pushed screens. The signed-out group contains the auth Stack. The signed-in group contains the tabs and the restaurant, order and configurator screens.
![The root Stack contains two protected groups. The signed-out guard contains the auth Stack with Log in and Create account. The signed-in guard contains the tabs for Restaurants, Orders and Cart, plus restaurant/[id] and orders/[id] as pushed screens and item/[id] as a modal.](/zomato/diagrams/w4-route-tree-light.png)
![The root Stack contains two protected groups. The signed-out guard contains the auth Stack with Log in and Create account. The signed-in guard contains the tabs for Restaurants, Orders and Cart, plus restaurant/[id] and orders/[id] as pushed screens and item/[id] as a modal.](/zomato/diagrams/w4-route-tree-dark.png)
The route tree has a deliberate separation:
- The root Stack owns the authentication guards.
- The auth group owns the Log in and Create account Stack.
- The tabs group owns the Restaurants, Orders and Cart tabs.
- The restaurant and order detail screens sit above the tabs in the root Stack.
- The item configurator sits in the root Stack and uses modal presentation.
This means the tab bar remains available while switching between the three primary screens. Opening a restaurant menu covers the tab bar. Opening an item configurator covers the menu as a modal.
The root layout can also contain app-wide providers such as TanStack Query's QueryClientProvider. Keep the navigator as a child of the providers whose context it reads.
Deep links
A deep link opens a particular route from outside the current navigation flow. A customer might tap a notification that points to an order or open a restaurant URL from another app.
The Expo app registers its own scheme through scheme in app.config.ts. For example, the configured zomatoprep scheme can identify app routes in development and release builds.
Expo Go uses its own development URL instead. For a restaurant route, the emulator can open a URL in this form:
exp://<LAN IP>:8081/--/restaurant/1The route is still matched against the same file tree. It does not need a separate screen implementation for deep links.
The authentication guard must apply to deep links too. If a signed-out customer opens the restaurant URL, the protected restaurant screen must not become available. The customer lands on the signed-out navigation instead.
Applying navigation to the Expo app
The Expo app shell brings these pieces together. The root Stack owns the authentication boundary. The (auth) and (tabs) groups provide separate navigation layouts. Dynamic routes represent restaurant and order IDs without requiring a new file for each record.
The Expo app shell exercise asks you to create this structure and verify it on an emulator and a phone. You will also check that stored authentication state controls which routes can open, including when the app receives a deep link.
Keep the distinction between route organization, navigation presentation and authorization clear. Route files determine which screens exist. Layouts select their navigators. Protected routes determine which screens are currently reachable.
Check your understanding
The Orders screen is stored at app/(tabs)/orders.tsx. A developer creates a link to /(tabs)/orders, but Expo Router cannot find the route. What should the link target be?
References
Everything above is explained on this page; these are where it comes from (Expo Router (Expo SDK 58)).