A layout that returns Stack or Tabs hands the routes of its folder to a navigator, which draws the header, the back button or the tab bar. The two treat the files in their folder differently: a Stack accepts every file without being told about it, while Tabs shows only the screens its layout declares. When a tab needs more than one screen, a common pattern is to nest a stack inside that tab.

Key terms

  • Stack: the expo-router navigator that pushes pages on top of each other and renders a header with a back button by default.
  • Tabs: the JavaScript tab navigator, imported from expo-router/js-tabs, that shows each screen its layout declares as a tab.
  • Stack.Screen and Tabs.Screen: components placed inside a navigator that declare a screen by its route name and set its options.

Stacks push pages as you open them

Return <Stack /> from a layout and the routes of that folder form a stack. That works in the root layout and in the layout of any folder. Take src/app/recipes/ with _layout.tsx, index.tsx and favorites.tsx. Opening /recipes goes to index.tsx first. Opening /recipes/favorites from there pushes that page onto the stack, and the header shows a back button that pops it and returns to the list. A page that is still on the stack keeps being rendered even while another page covers it.

Files in the folder become routes automatically, so a Stack doesn't need a screen declared for each one. Declare a screen with Stack.Screen when you want to set its options. Its name prop is required inside a layout and must match the route name, which is relative to the layout. A name matches a route with that exact name, or the index route of a folder with that name, so name="saved" also matches saved/index.tsx. A name that matches no route logs a [Layout children] warning, and that declaration is ignored.

The RecipesLayout example declares one screen to give it a title and leaves the list undeclared. The line <Stack.Screen name="favorites" options={{ title: 'Favorites' }} /> sets the title of that one page, and index.tsx still opens at /recipes even though no Stack.Screen names it.

Tabs need every screen declared

Import Tabs from expo-router/js-tabs. Importing it from expo-router is deprecated.

In SDK 58, JavaScript tabs show only the screens declared in the layout. Every file route is still registered in the tab navigator's state, but only routes declared with Tabs.Screen become tab items. The Layout guide on the docs site still says every route directly inside the folder becomes a tab, and its example still imports Tabs from expo-router. If you follow it, the files you leave out of the layout get no tab. A Tabs layout that declares no screens at all renders nothing, and warns in development that only declared screens become visible.

The declarations also set the order of the tabs and each tab's title and icon, and the index route is the tab selected by default. Put the tab pages in a src/app/(tabs) group with an index.tsx, and / opens src/app/(tabs)/index.tsx directly. If the focused route has no tab, for example after navigating to the path of an undeclared file, the tab navigator replaces it with the initial tab (or the first visible one) and keeps the unreachable route out of history.

Giving one tab its own stack

When a tab needs more than one screen, a stack inside that tab is often the way to go. Give the tab a folder whose layout returns a Stack, and declare that folder in the Tabs layout by its route name. In this tree for a cooking app, recipes/_layout.tsx is the RecipesLayout from earlier:

text
src/app/_layout.tsx
src/app/(tabs)/_layout.tsx
src/app/(tabs)/index.tsx
src/app/(tabs)/recipes/_layout.tsx
src/app/(tabs)/recipes/index.tsx
src/app/(tabs)/recipes/favorites.tsx
src/app/(tabs)/shopping.tsx

The TabLayout example is src/app/(tabs)/_layout.tsx for that tree. It declares two tabs and hides the header of the tab navigator.

<Tabs.Screen name="recipes" options={{ title: 'Recipes' }} /> matches the route named recipes, which is the nested recipes/_layout.tsx. Opening /recipes/favorites pushes favorites onto the recipes stack and leaves the tab bar visible. Each navigator adds its own header, so screenOptions={{ headerShown: false }} on Tabs hides the outer one and leaves the stack's header, with its back button, in place. shopping.tsx is not declared, so it gets no tab, and opening /shopping lands on the index tab instead.

The root layout can wrap all of this in another stack. Tabs nested inside an outer stack are often used to show modals over the tabs. That root Stack declares the group by name and hides its header with <Stack.Screen name="(tabs)" options={{ headerShown: false }} />, so only the recipes stack draws one.

Code Examples

tsx
import { Stack } from 'expo-router';

export default function RecipesLayout() {
  return (
    <Stack>
      <Stack.Screen name="favorites" options={{ title: 'Favorites' }} />
    </Stack>
  );
}
tsx
import { Tabs } from 'expo-router/js-tabs';

export default function TabLayout() {
  return (
    <Tabs screenOptions={{ headerShown: false }}>
      <Tabs.Screen name="index" options={{ title: 'Today' }} />
      <Tabs.Screen name="recipes" options={{ title: 'Recipes' }} />
    </Tabs>
  );
}
✓ Completed