Skip to main content
Version: 7.x

Screen

A screen represents routes in a navigator. A screen's configuration contains the component for the route, options, event listeners, etc.

Screens can be defined under the screens key in the navigator configuration:

const MyStack = createNativeStackNavigator({
screens: {
Home: HomeScreen,
Profile: ProfileScreen,
},
});

Configuration

Name

The name to use for the screen.

The key in the screens object is used as the name:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
},
},
});

This name is used to navigate to the screen:

navigation.navigate('Profile');

It is also used for the name property in the route.

While it is supported, we recommend avoiding spaces or special characters in screen names and keeping them simple.

Options

Options are used to configure how the screen gets presented in the navigator. It accepts either an object or a function returning an object:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
options: {
title: 'Awesome app',
},
},
},
});

When you pass a function, it'll receive the route, navigation and theme as arguments:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
options: ({ route, navigation, theme }) => ({
title: route.params.userId,
}),
},
},
});

See Options for screens for more details and examples.

Initial params

Initial params are used as the default params for the screen. If a screen is used as initialRouteName, it'll contain the params from initialParams. If you navigate to a new screen, the params passed are shallow merged with the initial params.

const Stack = createNativeStackNavigator({
screens: {
Details: {
screen: DetailsScreen,
initialParams: { itemId: 42 },
},
},
});

ID

A screen can have an ID to identify it uniquely. This is useful when you want to ensure that the screen with the same ID doesn't appear multiple times in the stack.

This can be done by specifying the getId callback. It receives an object with the route params:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
getId: ({ params }) => params.userId,
},
},
});

By default, navigate('ScreenName', params) updates the current screen if the screen name matches, otherwise adds a new screen in a stack navigator. So if you're on ScreenName and navigate to ScreenName again, it won't add a new screen even if the params are different - it'll update the current screen with the new params instead:

// Let's say you're on `Home` screen
// Then you navigate to `Profile` screen with `userId: 1`
navigation.navigate('Profile', { userId: 1 });

// Now the stack will have: `Home` -> `Profile` with `userId: 1`

// Then you navigate to `Profile` screen again with `userId: 2`
navigation.navigate('Profile', { userId: 2 });

// The stack will now have: `Home` -> `Profile` with `userId: 2`

If you specify getId and it doesn't return undefined, the screen is identified by both the screen name and the returned ID. That means that if you're on ScreenName and navigate to ScreenName again with different params - and return a different ID from the getId callback, it'll add a new screen to the stack:

// Let's say you're on `Home` screen
// Then you navigate to `Profile` screen with `userId: 1`
navigation.navigate('Profile', { userId: 1 });

// Now the stack will have: `Home` -> `Profile` with `userId: 1`

// Then you navigate to `Profile` screen again with `userId: 2`
navigation.navigate('Profile', { userId: 2 });

// The stack will now have: `Home` -> `Profile` with `userId: 1` -> `Profile` with `userId: 2`

The getId callback can also be used to ensure that the screen with the same ID doesn't appear multiple times in the stack:

// Let's say you have a stack with the screens: `Home` -> `Profile` with `userId: 1` -> `Settings`
// Then you navigate to `Profile` screen with `userId: 1` again
navigation.navigate('Profile', { userId: 1 });

// Now the stack will have: `Home` -> `Profile` with `userId: 1`

In the above examples, params.userId is used as an ID, subsequent navigation to the screen with the same userId will navigate to the existing screen instead of adding a new one to the stack. If the navigation was with a different userId, then it'll add a new screen.

If getId is specified in a tab or drawer navigator, the screen will remount if the ID changes.

Component

Each screen must specify a component to render for that route.

It can be passed under the screen property in the screen configuration:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
},
},
});

Layout

A layout is a wrapper around the screen. It makes it easier to provide things such as an error boundary and suspense fallback for a screen:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
layout: ({ children }) => (
<ErrorBoundary>
<React.Suspense
fallback={
<View style={styles.fallback}>
<Text style={styles.text}>Loading…</Text>
</View>
}
>
{children}
</React.Suspense>
</ErrorBoundary>
),
},
},
});

A navigation key is an optional key for this screen. This doesn't need to be unique. If the key changes, existing screens with this name will be removed (if used in a stack navigator) or reset (if used in a tab or drawer navigator).

This can be useful when we have some screens that we want to be removed or reset when the condition changes:

const Stack = createNativeStackNavigator({
screens: {
Profile: {
screen: ProfileScreen,
navigationKey: 'user',
},
},
});

For the static API, we recommend using the groups instead of the navigationKey for each screen as you can dynamically add or remove groups with the if property.

Event listeners

Event listeners can be used to subscribe to various events emitted for the screen. See listeners prop on Screen for more details.