Two things can go wrong between a URL and a screen: no file in src/app answers the URL, or the file answers it and its component throws. Expo Router shows a default screen for the first case, and you replace it with a +not-found route file. For the second case, you add an ErrorBoundary export to the route so it renders a fallback in place of the screen that threw. Both let you decide what the user reads when something breaks.
Key terms
- +not-found: the special route file
src/app/+not-found.tsx, displayed when the user navigates to a route that doesn't exist in the app. - Unmatched: the component exported by
expo-routerthat renders the default screen for unmatched routes. - ErrorBoundary: a named export from a route file that intercepts component-level errors in that route and renders in place of it.
- ErrorBoundaryProps: the type of the props an
ErrorBoundaryreceives,errorandretry.
What Expo Router shows when nothing matches
Native apps don't have a server, so strictly speaking they have no 404s. A native app can still be asked for a path it doesn't have, so Expo Router handles missing routes automatically in every app. If the root of src/app has no +not-found file, Expo Router generates one whose screen is Unmatched.
That default screen sets the screen title to "Not Found", shows the heading "Unmatched Route" with "Page could not be found." under it, and offers a way out. Its back action goes back when there is a screen to return to, and otherwise replaces the current screen with /.
Writing your own +not-found.tsx
File names that start with + have a special meaning in Expo Router, and +not-found catches any request that doesn't match a route in the app. Expo Router turns that segment into a catch-all pattern, *not-found, which is why one file at the root of src/app covers every unknown path.
Like any route, +not-found.tsx default-exports a component. Re-exporting Unmatched from expo-router as the default keeps the stock screen, and any other component replaces it. The NotFoundScreen example is a replacement written in the app's own words. Give a screen like this a link back to / so users can return to the home screen. The Link component that does it is covered in the section on navigating.
On the web, a not-found route is served with a 404 status code.
ErrorBoundary: a fallback for one route
Any route can export a component named ErrorBoundary next to its default export. Expo Router reads that named export from the route module separately from the screen, and wraps the screen in a React error boundary that uses it. When the screen throws, the boundary renders your ErrorBoundary in its place and passes it two props:
error: theErrorthat was thrown.retry: a function returningPromise<void>that clears the error state and renders the route component again.
In the StatsScreen example, averagePages throws when no reading sessions are logged, and the boundary takes over with the message and a button:
tsxexport function ErrorBoundary({ error, retry }: ErrorBoundaryProps) {
ErrorBoundaryProps is a type export of expo-router, imported with import { type ErrorBoundaryProps } from 'expo-router';. Pressing the button calls retry, which renders StatsScreen again. With the session list still empty it throws again and the fallback comes back, so retry helps only when whatever caused the error has changed in the meantime.
The export name has to be exactly ErrorBoundary. A route without its own ErrorBoundary passes the error up to the nearest parent's ErrorBoundary. When you don't need a custom fallback, expo-router also exports a ready-made ErrorBoundary component that takes the same error and retry props, so a route can re-export that one.
Code Examples
import { StyleSheet, Text, View } from 'react-native';
// src/app/+not-found.tsx: shown for any URL no other route matches
export default function NotFoundScreen() {
return (
<View style={styles.container}>
<Text style={styles.title}>This page is not on the shelf</Text>
<Text>Check the address and try again.</Text>
</View>
);
}
const styles = StyleSheet.create({
container: { flex: 1, alignItems: 'center', justifyContent: 'center', padding: 24 },
title: { fontSize: 20, fontWeight: '600', marginBottom: 8 },
});import { Button, Text, View } from 'react-native';
import { type ErrorBoundaryProps } from 'expo-router';
const sessionPages: number[] = [];
function averagePages(pages: number[]): number {
if (pages.length === 0) {
throw new Error('No reading sessions logged yet');
}
return pages.reduce((sum, count) => sum + count, 0) / pages.length;
}
// Rendered in place of StatsScreen when it throws
export function ErrorBoundary({ error, retry }: ErrorBoundaryProps) {
return (
<View>
<Text>Your stats could not be drawn: {error.message}</Text>
<Button title="Try again" onPress={retry} />
</View>
);
}
// src/app/(reader)/stats.tsx answers /stats
export default function StatsScreen() {
const average = averagePages(sessionPages);
return (
<View>
<Text>{average.toFixed(1)} pages per session</Text>
</View>
);
}