Skip to content
Tutorials Center
My learning

Topics

Lesson 7 of 38 ยท Expo Router and Navigation

Layouts and Stack Navigation

Use _layout files to wrap screens, configure the Stack navigator, customize headers and screen options, present modals and navigate with push, replace and back.

Every folder in app/ can contain a _layout.tsx. A layout is not a screen: it is the navigator that wraps the routes beside it. The most common one is Stack, which behaves like a pile of cards: opening a screen pushes a card on top, going back removes it, and the header shows a back button for you.

Expo Router and its navigators need the real runtime, so layouts are read-only code. The live examples model the stack and the layout-resolution rules.

The root layout

TSX
// app/_layout.tsx
import { Stack } from 'expo-router';

export default function RootLayout() {
  return (
    <Stack>
      <Stack.Screen name="index" options={{ title: 'Home' }} />
      <Stack.Screen name="details" options={{ title: 'Details' }} />
    </Stack>
  );
}

app/_layout.tsx is the root: everything is rendered inside it. It is also the place for providers (theme, auth, query client), font loading and the splash screen. Stack.Screen entries are optional; every route file inside the folder is registered automatically, and you only list a screen when you want to set its options. The name is the file path relative to the layout ("index", "details", "settings/profile").

Default options for all screens

TSX
<Stack
  screenOptions={{
    headerStyle: { backgroundColor: '#0b5fff' },
    headerTintColor: '#fff',
    headerTitleStyle: { fontWeight: '700' },
    animation: 'slide_from_right',
  }}
>
  <Stack.Screen name="index" options={{ title: 'Trails' }} />
  <Stack.Screen name="modal" options={{ presentation: 'modal', title: 'New trail' }} />
</Stack>

Useful options: title, headerShown: false, headerLargeTitle (iOS), headerBackTitle, headerRight / headerLeft (render buttons), presentation ('card', 'modal', 'transparentModal', 'formSheet'), animation, gestureEnabled, contentStyle.

Setting options from inside a screen

A screen can set its own options, even from data it loaded, with Stack.Screen or the useNavigation hook:

TSX
import { Stack, useLocalSearchParams } from 'expo-router';
import { Text, View } from 'react-native';

export default function Post() {
  const { id } = useLocalSearchParams<{ id: string }>();
  return (
    <View>
      <Stack.Screen options={{ title: `Post ${id}` }} />
      <Text>Post number {id}</Text>
    </View>
  );
}

Moving between screens

TSX
import { router, useRouter } from 'expo-router';

router.push('/details');          // add a screen on top
router.replace('/login');         // swap the current screen (no way back)
router.back();                    // pop
router.dismiss();                 // close a modal / pop one
router.dismissAll();              // back to the first screen of the stack
router.navigate('/details');      // go there; reuse an existing screen if present
router.canGoBack();               // boolean

The difference between push and navigate: push always adds a new card (even for the same route), while navigate goes to the existing instance if the route is already in the stack. Use replace after login or onboarding so the back button does not return to the finished flow. Declarative links with <Link> come in Links, Redirects and Deep Links.

A stack, modeled

This stand-in implements push, replace, back, navigate and dismissAll on an array, with the same semantics.

JS Stack navigation semantics

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Nested layouts

Layouts can nest. A route is rendered inside every _layout between the root and its folder:

Text
app/
  _layout.tsx            Stack (root)
  index.tsx
  (app)/
    _layout.tsx          another Stack or Tabs
    feed.tsx
    profile/
      _layout.tsx        a Stack just for the profile section
      index.tsx
      edit.tsx

The next stand-in finds the chain of layouts for a route, which also tells you which providers and navigators wrap it.

JS Layout chain for a route

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Initial route and the back stack

When a user opens a deep link straight to a deep screen, the stack contains only that screen, so there is nothing to go back to. Set unstable_settings.initialRouteName in the layout (or use groups) to define what sits underneath:

TSX
export const unstable_settings = {
  initialRouteName: 'index',
};

Modals

Create app/modal.tsx and register it with presentation: 'modal' in the root stack. Open it with router.push('/modal') and close it with router.dismiss(). A modal sits above tabs, because the root stack is above the tab layout.

TSX
<Stack>
  <Stack.Screen name="(tabs)" options={{ headerShown: false }} />
  <Stack.Screen name="modal" options={{ presentation: 'modal' }} />
</Stack>

Common mistakes

  • Putting a header-less layout (headerShown: false) at two levels and getting a double header (or none). Decide which navigator owns the header.
  • Using push for tabs or for "go to home", which grows the stack. Prefer navigate/replace.
  • Wrapping <Stack> in other views that break its size; a navigator should fill the screen.
  • Initializing providers inside a screen rather than in the layout, so they are recreated on navigation.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a _layout.tsx file?

    _layout files hold navigators (Stack, Tabs) and shared providers; they are not screens.
  2. 2. Which call leaves no way back to the current screen?

    replace swaps the current screen in the stack.
  3. 3. Where do global providers such as a theme provider usually go?

    The root layout wraps every route, so providers there are created once.
  4. 4. How do you show a screen as a modal?

    The presentation option controls how the stack shows the screen.

Summary

  • _layout.tsx holds the navigator (Stack, Tabs) and providers for its folder.
  • screenOptions set defaults; Stack.Screen options customize single screens, including from inside them.
  • push adds, replace swaps, navigate reuses, back/dismiss pop.
  • Layouts nest; a route sits inside every layout above it.

Next: Tabs and Drawer