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.
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.
To install React Navigation using the commandline, execute these commands before altering any of your app's code.
npm install @react-navigation/native
expo install react-native-gesture-handler react-native-reanimated react-native-screens react-native-safe-area-context @react-native-community/masked-view
npm install @react-navigation/stack
You don't need to install these libraries if you are making an Expo Snack. However, when you add your import statements, the Snack will ask if you want to install these libraries. Say yes!
Snack users can still use the technique shown in the tutorial, with these adjustments:
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
{
"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"
}
}
Here's a simple demo of screen navigation in a Snack.
⚠️ The Web preview may not work; load this demo on your phone instead.
import React from "react";
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import { View, Text, StyleSheet, Button } from "react-native";
const WelcomeScreen = ({ navigation }) => {
return (
<View style={styles.welcomeContainer}>
<Text>Welcome</Text>
</View>
);
}
const styles = StyleSheet.create({
welcomeContainer: {
flex: 1,
alignItems: "center",
justifyContent: "center",
backgroundColor: "paleturquoise"
}
});
export default WelcomeScreen;
import React from "react";
import { NavigationContainer } from '@react-navigation/native';
import { createStackNavigator } from '@react-navigation/stack';
import WelcomeScreen from "./screens/WelcomeScreen" ;
import PetsScreen from "./screens/PetsScreen" ;
import React from "react" ;
import { Text, View, StyleSheet, Image, ScrollView } from "react-native";
import Constants from "expo-constants";
import Card from "../components/Card";
const PetsScreen = () => {
// Put the JavaScript previously in the App function here.
return (
// Put the JSX previously in the App function here.
}
...
const styles = StyleSheet.create({
// Put the styles previously in App.js here.
});
export default PetsScreen;
import React from "react" ;
import { NavigationContainer } from "@react-navigation/native";
import { createStackNavigator } from "@react-navigation/stack";
import WelcomeScreen from "./screens/WelcomeScreen";
import PetsScreen from "./screens/PetsScreen";
export default function App() {}
const Stack = createStackNavigator();
export default function App() {
return (
<NavigationContainer>
</NavigationContainer>
);
}
export default function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Welcome">
<Stack.Screen name="Welcome" component={ WelcomeScreen } />
<Stack.Screen name="Pets" component={ PetsScreen } />
</Stack.Navigator>
</NavigationContainer>
);
}
...
import { View, Text, StyleSheet, Button } from "react-native";
const WelcomeScreen = () => {
return (
<View style={styles.welcomeContainer}>
<Button
title="See lost pets"
/>
</View>
);
}
...
export default WelcomeScreen;
...
const WelcomeScreen = ({ navigation }) => { // Note the added { navigation } parameter.
return (
<View style={styles.welcomeContainer}>
<Button
title="See lost pets"
onPress={() => navigation.navigate("Pets")}
/>
</View>
);
}
...
export default WelcomeScreen;
<Stack.Navigator initialRouteName="Welcome">
<Stack.Screen
name="Welcome"
component={WelcomeScreen}
options={
{
title: "Welcome",
headerStyle: {
backgroundColor: "teal"
},
headerTitleStyle: {
fontWeight: "bold",
color: "paleturquoise"
}
}
}
/>
<Stack.Screen
name="Pets"
component={PetsScreen}
options={
{
title: "Lost pets"
}
}
/>
</Stack.Navigator>
🧁 import { createDrawerNavigator } from '@react-navigation/drawer';
Or for CLI:marykate$ expo install @react-navigation/drawer
🧁 import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
Or for CLI: marykate$ expo install @react-navigation/bottom-tabs