Each route file in src/app renders one screen, but an app also needs things that sit around many screens at once: a navigator, a theme, a context provider. Expo Router puts those in _layout.tsx files, and the folder a layout sits in decides which screens it wraps. Reading a project's layout files tells you which navigator and which providers a given screen renders inside.
Key terms
- _layout.tsx: a special file in a folder of
src/appthat is not a page itself and defines how the routes in that folder are arranged. - Root layout: the
_layout.tsxdirectly insidesrc/app, which renders before any other route and is the entry point for the app's navigation. - Navigator: a component such as a stack navigator or a tab navigator that a layout returns to arrange the routes of its folder.
One layout per folder
Any folder inside src/app, and src/app itself, can define a layout by containing a _layout.tsx file. Every other file in src/app default-exports a page. Layout files are the exception: their default export is a component that renders before whatever page of that folder the user navigates to.
That component decides how the folder's pages relate to each other. To make them behave as a stack or as tabs, the layout returns a stack navigator or a tab navigator. A drawer or any other navigator goes in the same place. The user always lands on a page in the folder, and the layout renders first, around it.
What the root layout is for
Almost every app has a _layout.tsx directly inside src/app. This root layout renders before anything else in the app. With Expo Router, package.json sets main to expo-router/entry (the default template already does), and the first client file loaded is src/app/_layout.tsx.
So the setup code that used to go in an App.jsx file lives here: loading fonts, working with the splash screen and adding context providers. The root layout also returns the app's top-level navigator. The default template's root layout, for example, wraps the app in a ThemeProvider imported from expo-router, and picks DarkTheme or DefaultTheme for it from the device color scheme.
The RootLayout example has the same shape, for a shop app with a cart. It sets up two providers and returns a navigator.
<ThemeProvider value={DarkTheme}> fixes the theme to dark, where the template computes the value from the color scheme instead. CartProvider is your own context, and because the root layout renders around every route, every screen in the app renders inside it. <Stack /> is the top-level navigator, which the Stack and Tabs lesson of this section covers.
If src/app has no top-level _layout file, Expo Router generates a default root layout in its place. Write your own as soon as the app has a provider or a navigator to set up.
Layouts inside layouts
A layout deeper in the tree doesn't replace the root layout. It nests inside it. When Expo Router builds the route tree, a folder with its own _layout becomes the layout for the routes below it, and that layout is added as a child of the parent layout. Routes in a folder without a _layout are hoisted to the nearest _layout above them.
Take this tree for the same shop app:
textsrc/app/_layout.tsx src/app/index.tsx src/app/(account)/_layout.tsx src/app/(account)/profile.tsx src/app/(account)/billing/invoices.tsx src/app/orders/_layout.tsx src/app/orders/index.tsx src/app/orders/returns.tsx
For /orders/returns, three files render, outermost first: src/app/_layout.tsx, then src/app/orders/_layout.tsx, then returns.tsx. For /profile, the (account) group stays out of the URL, but its layout still renders between the root layout and profile.tsx. The billing folder has no layout, so invoices.tsx is hoisted to the (account) layout, and /billing/invoices renders inside the same two layouts as /profile.
Route names inside a layout are relative to that layout. The root layout sees orders/_layout.tsx as one route named orders, the path with _layout removed. The (account) layout sees billing/invoices. These names are what a navigator uses when you declare its screens.
A folder doesn't always need a layout
Pages in a folder without a layout still join the parent's navigator. Say orders/_layout.tsx returns a stack and you add src/app/orders/archive/index.tsx. That page is pushed onto the orders stack, and no archive/_layout.tsx is involved. Adding one that returns a Stack would create a second stack inside the first.
Nest navigators only when the screens need one. A folder that only shapes the URL can share the navigator of the folder above it.
Code Examples
import { DarkTheme, Stack, ThemeProvider } from 'expo-router';
import { CartProvider } from '@/context/cart';
export default function RootLayout() {
return (
<ThemeProvider value={DarkTheme}>
<CartProvider>
<Stack />
</CartProvider>
</ThemeProvider>
);
}