Getting started
If you're already familiar with React Native then you'll be able to get moving with React Navigation quickly! If not, you may want to read sections 1 to 4 (inclusive) of React Native Express first, then come back here when you're done.
What follows within the Fundamentals section of this documentation is a tour of the most important aspects of React Navigation. It should cover enough for you to know how to build your typical small mobile application, and give you the background that you need to dive deeper into the more advanced parts of React Navigation.
Installation
Install the required packages in your React Native project:
- npm
- Yarn
npm install @react-navigation/native
React Navigation is made up of some core utilities and those are then used by navigators to create the navigation structure in your app. Don't worry too much about this for now, it'll become clear soon enough! To frontload the installation work, let's also install and configure dependencies used by most navigators, then we can move forward with starting to write some code.
The libraries we will install now are react-native-gesture-handler
, react-native-reanimated
, react-native-screens
and react-native-safe-area-context
and @react-native-community/masked-view
. If you already have these libraries installed and at the latest version, you are done here! Otherwise, read on.
Installing dependencies into an Expo managed project
In your project directory, run:
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
This will install versions of these libraries that are compatible.
You can now continue to "Hello React Navigation" to start writing some code.
Installing dependencies into a bare React Native project
In your project directory, run:
- npm
- Yarn
npm install react-native-reanimated react-native-gesture-handler react-native-screens react-native-safe-area-context @react-native-community/masked-view
Note: You might get warnings related to peer dependencies after installation. They are usually caused by incorrect version ranges specified in some packages. You can safely ignore most warnings as long as your app builds.
From React Native 0.60 and higher, linking is automatic. So you don't need to run react-native link
.
If you're on a Mac and developing for iOS, you need to install the pods (via Cocoapods) to complete the linking.
npx pod-install ios
To finalize installation of react-native-gesture-handler
, add the following at the top (make sure it's at the top and there's nothing else before it) of your entry file, such as index.js
or App.js
:
import 'react-native-gesture-handler';
Note: If you skip this step, your app may crash in production even if it works fine in development.
Now, we need to wrap the whole app in NavigationContainer
. Usually you'd do this in your entry file, such as index.js
or App.js
:
import 'react-native-gesture-handler';import * as React from 'react';import { NavigationContainer } from '@react-navigation/native';
export default function App() { return ( <NavigationContainer>{/* Rest of your app code */}</NavigationContainer> );}
Note: When you use a navigator (such as stack navigator), you'll need to follow the installation instructions of that navigator for any additional dependencies. If you're getting an error "Unable to resolve module", you need to install that module in your project.
Now you are ready to build and run your app on the device/simulator.
Continue to "Hello React Navigation" to start writing some code.