Skip to main content

Hello React Navigation

In a web browser, clicking a link pushes a page onto the browser's history stack, and pressing the back button pops the page from the stack, making the previous page active again. React Native doesn't have a built-in history like a web browser - this is where React Navigation comes in.

The native stack navigator keeps track of visited screens in a history stack. It also provides UI elements such as headers, native gestures, and animations to transition between screens etc. that you'd expect in a mobile app.

Installing the native stack navigator library

Each navigator in React Navigation lives in its own library.

To use the native stack navigator, we need to install @react-navigation/native-stack:

npm install @react-navigation/native-stack
info

@react-navigation/native-stack depends on react-native-screens and the other libraries that we installed in Getting started. If you haven't installed those yet, head over to that page and follow the installation instructions.

Installing the elements library

The @react-navigation/elements library provides components designed to work with React Navigation. In this guide, we'll use components like Button from the elements library:

npm install @react-navigation/elements

Creating a native stack navigator

We can create a native stack navigator by using the createNativeStackNavigator function:

import * as React from 'react';
import { View, Text } from 'react-native';
import { createStaticNavigation } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}

const RootStack = createNativeStackNavigator({
screens: {
Home: HomeScreen,
},
});

const Navigation = createStaticNavigation(RootStack);

export default function App() {
return <Navigation />;
}

createNativeStackNavigator takes a configuration object containing the screens to include, as well as various other options.

createStaticNavigation takes the navigator and returns a component to render in the app. It should only be called once, typically at the root of your app (e.g., in App.tsx):

warning

In a typical React Native app, the createStaticNavigation function should be only used once in your app at the root.

Basic app using stack navigator

If you run this code, you will see a screen with an empty navigation bar and a grey content area containing your HomeScreen component (shown above). These are the default styles for a stack navigator - we'll learn how to customize them later.

tip

The casing of the route name doesn't matter -- you can use lowercase home or capitalized Home, it's up to you. We prefer capitalizing our route names.

Configuring the navigator

We haven't passed any configuration to the navigator yet, so it just uses the default configuration.

Let's add a second screen and configure Home as the initial route:

const RootStack = createNativeStackNavigator({
initialRouteName: 'Home',
screens: {
Home: HomeScreen,
Details: DetailsScreen,
},
});

Now our stack has two routes: Home and Details. Routes are defined under the screens property - the property name is the route name, and the value is the component to render.

Here, the initial route is set to Home. Try changing initialRouteName to Details and reload the app (Fast Refresh won't pick up this change) to see the Details screen first.

Specifying options

Each screen can specify options such as the header title.

We can specify the options property in the screen configuration to set screen-specific options:

To specify the options, we'll change how we have specified the screen component. Instead of specifying the screen component as the value, we can also specify an object with a screen property:

const RootStack = createNativeStackNavigator({
initialRouteName: 'Home',
screens: {
Home: {
screen: HomeScreen,
},
Details: DetailsScreen,
},
});

This will let us specify additional options for the screen.

Now, we can add an options property:

const RootStack = createNativeStackNavigator({
initialRouteName: 'Home',
screens: {
Home: {
screen: HomeScreen,
options: {
title: 'Overview',
},
},
Details: DetailsScreen,
},
});

To apply the same options to all screens, we can use screenOptions on the navigator:

const RootStack = createNativeStackNavigator({
initialRouteName: 'Home',
screenOptions: {
headerStyle: { backgroundColor: 'tomato' },
},
screens: {
Home: {
screen: HomeScreen,
options: {
title: 'Overview',
},
},
Details: DetailsScreen,
},
});

Passing additional props

Passing additional props to a screen is not supported in the static API.

What's next?

Now that we have two screens, "How do we navigate from Home to Details?". That's covered in the next section.

Using with TypeScript

If you are using TypeScript, you will need to specify the types accordingly. You can check Type checking with TypeScript after going through the fundamentals for more details. For now, we won't be covering TypeScript in the examples.

Summary

  • React Native doesn't have a built-in API for navigation like a web browser does. React Navigation provides this for you, along with the iOS and Android gestures and animations to transition between screens.
  • createNativeStackNavigator is a function that takes the screens configuration and renders our content.
  • Each property under screens refers to the name of the route, and the value is the component to render for the route.
  • To specify what the initial route in a stack is, provide an initialRouteName option for the navigator.
  • To specify screen-specific options, we can specify an options property, and for common options, we can specify screenOptions.