How to Use Routing with React Navigation in React Native ?
Last Updated :
01 Aug, 2024
Almost every mobile application requires navigating between different screens. React Native provides an elegant and easy-to-use library to add navigation to native applications: react-navigation. It is one of the most popular libraries used for routing and navigating in a React Native application.
Transitioning between multiple screens is managed by Navigators. React Navigation allows various kinds of navigators, like Stack Navigators, Drawer Navigators, Tab Navigators, etc. Along with navigating between multiple screens, it can also be used for sharing data between them.
Approach: We will be using the StackNavigator provided by React Navigation. It is used to allow transitions between screens wherein every new screen is placed on top of a stack. In our example, we will be creating 3 screens and transitioning between them using the StackNavigator. We will also learn how to pass data from one screen to another and display it in the screen header along with basic transitioning.
Creating application and installing modules:
Step 1: Open your terminal and install expo-cli by the following command.
npm install -g expo-cli
Step 2: Now create a project by the following command.
expo init react-navigation-routing
Step 3: Now go into your project folder i.e. react-navigation-routing
cd react-navigation-routing
Step 4: Install the required packages using the following command:
npm install –save react-navigation react-navigation-stack react-native-reanimated react-native-gesture-handler react-native-screens react-native-vector-icons
Project Structure: The project directory should look like the following:

Example: In this example, we will create 3 screens, namely, Home Screen, Profile Screen, and Settings Screen. We will use a Stack Navigator and configure it with some basic styles. We will also dynamically send data from one screen and display it as the header title on another screen (take input from the user on the Home Screen, pass it on to the Profile Screen, and display it on the Header of the Profile Screen.
This file contains the basic Navigator setup. We will use the createStackNavigator() method provided by the react-navigation-stack library to create our stack navigator. Some basic styles are provided to all the 3 screens using the default navigation options.Â
App.js
import React from "react";
import { createAppContainer } from "react-navigation";
import { createStackNavigator } from "react-navigation-stack";
import HomeScreen from "./screens/HomeScreen";
import ProfileScreen from "./screens/ProfileScreen";
import SettingsScreen from "./screens/SettingsScreen";
const AppNavigator = createStackNavigator(
{
Home: HomeScreen,
Profile: ProfileScreen,
Settings: SettingsScreen,
},
{
defaultNavigationOptions: {
headerStyle: {
backgroundColor: "#006600",
},
headerTintColor: "#FFF",
},
}
);
const Navigator = createAppContainer(AppNavigator);
export default function App() {
return (
<Navigator>
<HomeScreen />
</Navigator>
);
}
This is the first screen of our stack. In this screen, we will ask the user to provide a profile name as an input which we will pass to the "Profile" screen.
HomeScreen.js
import React, { useState } from "react";
import { Text, View, TextInput, Button } from "react-native";
import { Ionicons } from "@expo/vector-icons";
const Home = (props) => {
const [input, setInput] = useState("");
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text style={{ color: "#006600", fontSize: 40 }}>Home Screen!</Text>
<Ionicons name="ios-home" size={80} color="#006600" />
<TextInput
placeholder="Enter your profile"
value={input}
onChangeText={(value) => setInput(value)}
/>
<Button
title="Go to Profile"
color="#006600"
onPress={() =>
props.navigation.navigate("Profile", { username: input })
}
/>
</View>
);
};
export default Home;
This screen will receive the input given by the user in the Home Screen using the navData object and display it in its header.Â
ProfileScreen.js
import React from "react";
import { Text, View, Button } from "react-native";
import { Ionicons } from "@expo/vector-icons";
const Profile = (props) => {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text style={{ color: "#006600", fontSize: 40 }}>Profile Screen!</Text>
<Ionicons name="ios-person-circle-outline" size={80} color="#006600" />
<Button
title="Go to Settings"
color="#006600"
onPress={() => props.navigation.navigate("Settings")}
/>
</View>
);
};
Profile.navigationOptions = (navData) => {
return {
headerTitle: navData.navigation.getParam("username"),
};
};
export default Profile;
This is a simple screen with a button to go back to the Home Screen.
SettingsScreen.js
import React from "react";
import { Text, View, Button } from "react-native";
import { Ionicons } from "@expo/vector-icons";
const Settings = (props) => {
return (
<View style={{ flex: 1, alignItems: "center", justifyContent: "center" }}>
<Text style={{ color: "#006600", fontSize: 40 }}>Settings Screen!</Text>
<Ionicons name="ios-settings-outline" size={80} color="#006600" />
<Button
title="Go to Home"
color="#006600"
onPress={() => props.navigation.navigate("Home")}
/>
</View>
);
};
export default Settings;
Step to run the application: Start the server by using the following command.
expo start
Output:
Reference: https://reactnative.dev/docs/navigation
Similar Reads
How to Integrate React Router with Remix?
Integrating React Router with Remix includes the usage of Remix's integrated router abilities thinking that Remix already uses React Router as its foundation. However, if you want to go beyond number one routing and leverage several React Router's extra precise or advanced skills.You may integrate t
7 min read
Implementing React Router with Redux in React
This article explores implementing React Router with Redux for enhanced state management and routing in React applications. Table of Content What is React Router and Redux?Approach to implement React Router in ReduxSteps to Setup the React AppWhat is React Router and Redux?React Router facilitates r
3 min read
React Suite Nav Used with Link in next/link
React Suite is a popular front-end library with a set of React components that are designed for the middle platform and back-end products. Nav Provides a list of various forms of navigation menus, which can be landscape and portrait layouts.  Nav Items can be used with Links also. These can be used
3 min read
React Native Top Tab Navigator
To create a Top Tab Navigator, we need to use the createMaterialTopTabNavigator function available in the react-navigation library. It is designed with the material theme tab bar on the top of the screen. It allows switching between various tabs by tapping them or swiping horizontally. Default trans
5 min read
How to Add Icons at the Bottom of Tab Navigation in React Native ?
Adding Icons at the Bottom of Tab Navigation in React Native is a fairly easy task. In this article, we will implement a basic application to learn to use icons in our tab navigation. For this, we first need to set up the application and install some packages.Implementation: Now letâs start with the
3 min read
How to programmatically navigate using React Router ?
React Router provides a way to navigate between different parts of an application when certain events occur, such as button clicks or form submissions. Programmatically navigating allows us to change the URL of an application without a full page reload, enabling a smoother user experience. We will d
3 min read
React Suite Pagination Used with Link in next/link
React suite is a library of React components, sensible UI design, and a friendly development experience. It is supported in all major browsers. It provides pre-built components of React which can be used easily in any web application. In this article, we'll learn about React suite Pagination Used wi
2 min read
Create Store Navigation using React and Tailwind CSS
We will build a simple Store Navigation system using React and Tailwind CSS and React Icons. The navigation bar will have links to different store sections such as Home and Products and Cart and Profile. Each link will be styled with icons accompanying them to make the UI more intuitive. We will als
4 min read
React Native Tab Navigation Component
In this article, we are going to see how to implement Tab Navigation in react-native. For this, we are going to use createBottomTabNavigator component. It is basically used for navigation from one page to another. These days mobile apps are made up of a single screen, so create various navigation co
3 min read
How to add navigation links to the React Bootstrap Navbar?
In ReactJS, we use Nabvar or Navigation Bar as the core component. For ease of navigation over the application, we use this NavBar in react-bootstrap. We need to add navigation links to go through different routes or pages of the application. So to add the navigation links to the React Bootstrap Nav
5 min read