React Navigation V7 in Expo
Introduction to React Navigation v7
Getting Started with React Navigation
When you build a mobile app, you need a way for users to move between different screens. This is where a navigation library comes in. For React Native, the most popular choice is React Navigation. It provides a simple way to set up common navigation patterns like stacks, tabs, and drawers.
Version 7 of React Navigation introduced some significant improvements that make it more powerful and easier to use. Let's dive into what's new and how to get it set up in your Expo project.
What's New in v7?
React Navigation v7 focuses on making navigation more predictable, performant, and developer-friendly. The most important change is the introduction of a new static API.
Before v7, you could define your app's screens dynamically. While flexible, this could sometimes lead to unexpected behavior. The new static API requires you to define all your navigation routes in one place, making your app's structure clearer and more stable.
// Old way (v6)
// Screens could be defined conditionally
<Stack.Navigator>
{isLoggedIn ? (
<Stack.Screen name="Profile" component={ProfileScreen} />
) : (
<Stack.Screen name="SignIn" component={SignInScreen} />
)}
</Stack.Navigator>
// New way (v7)
// All screens are defined upfront
<Stack.Navigator>
<Stack.Screen name="Profile" component={ProfileScreen} />
<Stack.Screen name="SignIn" component={SignInScreen} />
</Stack.Navigator>
This change allows React Navigation to understand your app's entire structure from the start. This enables two key improvements:
- Preloading Screens: The library can now preload screens before you even navigate to them, making your app feel much faster and more responsive.
- Type Safety: The static API provides much stronger TypeScript support out of the box. This means you get better autocompletion and can catch errors related to navigation at compile time, not runtime.
Setting Up Your Project
Let's get React Navigation set up in a new Expo project. First, you'll need to install the necessary packages. Open your terminal in your project's root directory and run the following command:
npx expo install @react-navigation/native @react-navigation/native-stack react-native-screens react-native-safe-area-context
Using npx expo install instead of npm install or yarn add is important because it ensures you get the versions of these packages that are compatible with your Expo SDK version.
Once the packages are installed, you need to wrap your entire app in a NavigationContainer. This component manages your navigation tree and contains the navigation state. Let's modify your main App.js file.
import * as React from 'react';
import { View, Text } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
// Define a simple screen component
function HomeScreen() {
return (
<View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
<Text>Home Screen</Text>
</View>
);
}
// Create a stack navigator
const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}
export default App;
In this example, we import NavigationContainer to wrap our app's navigation structure. We use createNativeStackNavigator to create our navigator. Inside the <Stack.Navigator>, each <Stack.Screen> component defines a route in our navigation stack. The name prop is the unique key for the screen, and the component prop is the React component that will be rendered for that screen.
With this setup, your app will now display the HomeScreen with a default header at the top.
What is the primary role of the React Navigation library in a React Native application?
What is the most significant new feature introduced in React Navigation v7 that improves performance and type safety?