Just-in-Time Learning initiative

Version 1.3

These instructions are a condensed version of a screencast on how to move from one screen to another in React Native. We'll use the React Navigation library to add a stack and inside list some routes to various screens of your app.

Introduction

Once your app gets to a certain size, you're going to want different screens--a login screen, a home screen, maybe a list of records and a detail screen for each record. If you were making a website, you'd just add an HTML page for each screen and a link that you click on to go from one to the next. It's not quite as simple for mobile apps, but a third-party library called React Navigation makes things fairly easy.

One of the nice things this library gives you out of the box are some of the navigation transitions you've seen in the past that are common to mobile interface design. In this tutorial we're going to start with the simplest transition, called "stack," but also show you how to create several others too.

We'll assume you know how to install JavaScript libraries on the command line and how to make custom React Native components. We'll also assume you have some kind of app with an initial screen to work from.

In this tutorial we'll take a single-screen app that shows records of lost pets and start by moving that content to a "Pets" screen so we can launch with a "Welcome" screen instead. Users who tap on a button in the welcome screen will be taken to the records screen. We'll also do some basic customization of each screen's header.

⚠️ Remember that in multi-component apps, you may have to reload manually to see changes (eg, by simulating the shake gesture).

⚠️ Note that React Navigation is a third-party library that is frequently modified, so check ReactNavigation.org to see if the syntax in this tutorial has been updated.

Installing React Navigation

🖥 Option A: Expo Command Line Interface (CLI)

To install React Navigation using the commandline, execute these commands before altering any of your app's code.

🧁 Option B: Snack users

Snack users can still use the technique shown in the tutorial, with these adjustments:

  1. Instead of using the command line to add the navigation dependencies, just type them at the top of your Snack, eg:
  2. 
    import { NavigationContainer } from '@react-navigation/native';
    import { createStackNavigator } from '@react-navigation/stack';
    
  3. Then be sure to click the warnings at bottom to add each dependency as needed. By the time you're done, your package.json file will look something like this:
  4. 
    {
    	"dependencies": {
    		"@react-navigation/stack": "*",
    		"@react-navigation/native": "*",
    		"react-native-screens": "~3.29.0",
    		"react-native-gesture-handler": "~2.14.0",
    		"react-native-safe-area-context": "4.8.2",
    		"@react-native-community/masked-view": "^0.1.0"
    	}
    }
    
  5. As the tutorial explains, you'll create a screens folder with a .js file for each screen. For a Snack, use the left-hand pane (click Editor > Files at bottom to show or hide this).

Here's a simple demo of screen navigation in a Snack.

⚠️ The Web preview may not work; load this demo on your phone instead.

Creating your first screens

Create your navigation stack

Linking the screens

Customizing the header

Other React Navigation options