CodeBlanks Logo Icon CodeBlanks

React Navigation

Learn the React Navigation library.

What is React Navigation?

In React Native, screen transitions are handled by the React Navigation library. It manages the screen stack, tabs and drawer. Each screen is a component; transitions happen through the navigation object.

React Navigation is not part of the React Native core; it is installed as a separate package. The base package is @react-navigation/native. In Expo projects the dependencies are usually pre-installed, but installing them explicitly is still good practice.

Package installation

To use React Navigation you first install the @react-navigation/native package. For stack navigation you also need @react-navigation/native-stack. With Expo: npx expo install @react-navigation/native @react-navigation/native-stack.

These packages also require react-native-screens and react-native-safe-area-context; Expo adds them automatically. After installation you import the NavigationContainer component to build the root wrapper.

Practice Your turn: wrap App with NavigationContainer

Import NavigationContainer from @react-navigation/native. Wrap the root view returned by the App component with <NavigationContainer>.

Starter code

App.js
// import the container
// wrap the App component with the container
function App() {
  return null;
}

Sample solution

App.js
import { NavigationContainer } from '@react-navigation/native';

function App() {
  return (
    <NavigationContainer>
      {/* screens go here */}
    </NavigationContainer>
  );
}

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does React Navigation provide?

  • Network requests
  • Persistent data storage
  • Screen-to-screen transitions Correct answer
  • Style definitions

Explanation: React Navigation manages screen transitions and routing.

2 Fill in the blank: import NavigationContainer

Question
import { ____ } from '@react-navigation/native';
Answer
import { NavigationContainer } from '@react-navigation/native';

Explanation: NavigationContainer is the root wrapper component.

3 Match: package and purpose

Term Meaning
@react-navigation/native root wrapper
@react-navigation/native-stack stack navigation
react-native-screens screen optimization

All lessons in Beginner