Configuring Stack Screens

+15 Mana ✨

A stack layout gives every screen in its folder a header, and most of what that header shows comes from options. The same option can be set in three places: on the Stack for every screen, on a Stack.Screen in the layout for one screen, or from inside the route itself. Where you put it decides whether a value is shared, fixed for one route, or able to use data that only the screen has.

Key terms

  • screenOptions: a prop of Stack that holds the default options for every screen in that navigator, given as an object or as a function that returns one.
  • options: the prop of Stack.Screen that sets the options of one screen, such as title or headerShown.
  • headerLeft and headerRight: header options that take a function returning a React element, drawn on the left or the right side of the header.

Three sources, merged per screen

Each screen's options are merged in a fixed order, and every later source overrides the earlier ones for the keys it sets. First comes the navigator's screenOptions, then the options of that screen's Stack.Screen in the layout, then options set from inside the route. A screen ends up with the shared defaults plus whatever its own entries change.

Most header options are plain values. title is a string the header displays when no headerTitle is set, and without a title the header shows the route name. headerShown controls whether the header appears at all, and the header is shown by default. headerTintColor changes the color of the back button and the title, and headerStyle accepts only backgroundColor.

Defaults on the Stack

screenOptions suits a header style that every route in the stack shares. The NotesLayout example gives the whole notes folder a dark header with headerStyle: { backgroundColor: '#1e293b' } and headerTintColor: '#f8fafc'.

Every note screen gets those colors, including screens added to the folder later, without being declared. When a default has to depend on the route, screenOptions also accepts a function that receives route, navigation and theme and returns the options object.

One route, set from the layout

The line <Stack.Screen name="index" options={{ title: 'All notes' }} /> inside the layout's Stack configures that one route statically. Inside a layout, name is required. The screen's options win over screenOptions for any key both set, so one screen can change its tint or hide its header while its siblings keep the defaults.

In a layout, options can also be a function that receives the route and navigation objects. That lets the layout read a route's params, for example to build a title from an id in the URL. It can't read anything the screen loads after it mounts: a fetched note, a computed total, a value held in the screen's state.

Options only the route knows

For those values, render Stack.Screen inside the route component. There it draws nothing. It passes its options to navigation.setOptions, and because options set that way come last in the merge, they override both layout sources.

The NoteScreen example loads a note from its id and sets the title from it with title: note?.title ?? 'Note'. Until the note has loaded, the header shows the fallback string.

A Stack.Screen inside a route follows different rules from one in a layout. Leave out name: passing it throws The name prop on the Screen component may only be used when it is inside a Layout route. Pass options as a plain object. The function form works only in a layout. In development, a function passed from a route logs a warning and is replaced by an empty object, so none of its options apply.

A route file can't export its options either. Expo Router reads a fixed set of exports from a route module, such as the default component, ErrorBoundary and unstable_settings, and options is not one of them.

Buttons in the header

headerRight and headerLeft take a function and render the element it returns. The function receives props that include tintColor and canGoBack, which tells whether the stack can navigate back. A button can use them to match the header's tint, or to hide itself when there is nowhere to go back to.

headerLeft replaces the back button. To keep the back button next to your own element, also set headerBackVisible: true. It has no effect on the first screen in the stack.

A button that updates the screen's own state belongs in the route's Stack.Screen, where it can reach the state setter. In NoteScreen, headerRight renders a Pin button whose handler calls setPinned((current) => !current). A layout has no access to setPinned, so the same button couldn't be declared there.

Stack.Toolbar is another way to add header buttons. It belongs to the composition API, with Stack.Title and Stack.Header, which is still in alpha. Both styles can be used in the same project.

Code Examples

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

export default function NotesLayout() {
  return (
    <Stack
      screenOptions={{
        headerStyle: { backgroundColor: '#1e293b' },
        headerTintColor: '#f8fafc',
      }}
    >
      <Stack.Screen name="index" options={{ title: 'All notes' }} />
    </Stack>
  );
}
tsx
import { Stack, useLocalSearchParams } from 'expo-router';
import { useState } from 'react';
import { Button, Text, View } from 'react-native';

import { useNote } from '@/hooks/use-note';

export default function NoteScreen() {
  const { id } = useLocalSearchParams<{ id: string }>();
  const note = useNote(id);
  const [pinned, setPinned] = useState(false);

  return (
    <View>
      <Stack.Screen
        options={{
          title: note?.title ?? 'Note',
          headerRight: () => (
            <Button title="Pin" onPress={() => setPinned((current) => !current)} />
          ),
        }}
      />
      {pinned ? <Text>Pinned to the top of your list</Text> : null}
      <Text>{note?.body}</Text>
    </View>
  );
}
✓ Completed