Skip to main content
React Native Introduction
CHAPTER 14 Beginner

Navigation in React Native

Updated: May 16, 2026
5 min read

# CHAPTER 14

1. Introduction

A mobile application is rarely a single, static page. It is a complex web of screens: a Login screen that pushes to a Home feed, an item thumbnail that taps into a Detail view, and a side-drawer that reveals an Account page. In standard React Web development, routing is handled by reading the URL (e.g., reactrouter.com). Mobile apps do not have URLs; they use a physical "deck of cards" metaphor where screens are pushed on top of each other. In this chapter, we will master Navigation in React Native. We will install the absolute industry-standard library, React Navigation, and construct the foundational architecture required to move between screens.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Explain why React Native requires a third-party routing library.
  • Install the react-navigation core and its native dependencies.
  • Understand the concept of the NavigationContainer.
  • Understand the "Stack" (Push/Pop) navigation metaphor.
  • Architect the root App.js file to support a multi-screen application.

3. Why React Navigation?

React Native core *does not* include a built-in routing system. Years ago, there were many competing libraries. Today, the war is over. React Navigation (sponsored by Expo) is the undisputed industry standard. It binds directly to the underlying native iOS/Android navigation APIs, ensuring that screen transitions feel incredibly smooth and native (like the iOS swipe-from-left gesture).

4. Installation and Setup

Installing React Navigation requires multiple pieces, because it hooks deeply into native device features (like gestures and safe areas).

Step 1: Install the Core Library Open your terminal in your project folder and run:

bash
1
npm install @react-navigation/native

Step 2: Install Expo Native Dependencies Because these packages contain native C++ and Java code, Expo has a special command to install the perfect, compatible versions:

bash
1
npx expo install react-native-screens react-native-safe-area-context

Step 3: Install the Stack Navigator We want standard "push to new screen" functionality, so we install the Native Stack:

bash
1
npm install @react-navigation/native-stack

5. The Architecture (NavigationContainer)

For routing to work, your *entire application* must be wrapped inside a giant invisible box called the NavigationContainer. This box manages the hidden state of what screen is currently active, and maintains the history (so the "Back" button works).

Inside the Container, we place a Navigator. Inside the Navigator, we place our individual Screens.

6. The Stack Metaphor

React Navigation utilizes a "Stack" metaphor. Imagine your phone screen is a table.
  • When you open the app, it places the HomeScreen card on the table.
  • When you tap a product, it Pushes the DetailsScreen card *on top* of the Home card. You are now looking at Details.
  • When you tap the back arrow, it Pops (destroys) the Details card off the top of the stack, instantly revealing the HomeScreen card underneath it exactly as you left it.

7. Setting up the Root Navigator (App.js)

Let's wire everything together in App.js. We will create two dummy screens and configure the Stack.
javascript
12345678910111213141516171819202122232425262728293031323334353637
import React from 'react';
import { View, Text } from 'react-native';

// 1. IMPORT NAVIGATION TOOLS
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

// 2. CREATE DUMMY SCREENS
function HomeScreen() {
  return <View style={{ flex: 1, alignItems: &#039;center&#039;, justifyContent: &#039;center&#039; }}><Text>Home Screen</Text></View>;
}

function DetailsScreen() {
  return <View style={{ flex: 1, alignItems: &#039;center&#039;, justifyContent: &#039;center&#039; }}><Text>Details Screen</Text></View>;
}

// 3. INITIALIZE THE STACK OBJECT
const Stack = createNativeStackNavigator();

// 4. THE MAIN APP COMPONENT
export default function App() {
  return (
    // Wrap EVERYTHING in the NavigationContainer
    <NavigationContainer>
      
      {/* Define the Stack */}
      <Stack.Navigator initialRouteName="Home">
        
        {/* Register each screen with a unique 'name' string */}
        <Stack.Screen name="Home" component={HomeScreen} />
        <Stack.Screen name="Details" component={DetailsScreen} />
        
      </Stack.Navigator>
      
    </NavigationContainer>
  );
}

If you run this code, your app will display the HomeScreen. Notice that React Navigation automatically generated a beautiful white App Header bar with the word "Home" centered at the top!

8. Visual Learning: The Routing Tree

txt
1234567
[ NavigationContainer ] (Manages History/Back Button)
        |
   [ Stack.Navigator ] (The Deck of Cards)
        |
        |-- [ Stack.Screen name="Home" ]    <-- initialRouteName
        |
        |-- [ Stack.Screen name="Details" ]

9. Common Mistakes

  • Forgetting NavigationContainer: If you try to use <Stack.Navigator> directly in App.js without wrapping it in a <NavigationContainer>, the app will violently crash on launch. The engine requires the container to hold the global navigation state tree.

10. Best Practices

  • Screen Separation: In the code above, we defined HomeScreen and DetailsScreen in the same App.js file for demonstration. In a real application, you must create a screens/ folder, place HomeScreen.js in there, and import HomeScreen from './screens/HomeScreen' into App.js. Keep your routing file clean!

11. Practice Exercises

  1. 1. What specific component acts as the root state manager for the entire routing system and must wrap all navigators?
  1. 2. Using the deck of cards metaphor, what terminology is used when a user taps the "Back" button to destroy the current screen and reveal the previous one?

12. MCQs with Answers

Question 1

Why do we use the npx expo install command to install react-native-screens and react-native-safe-area-context instead of the standard npm install?

Question 2

When defining a <Stack.Screen>, what is the purpose of the name prop?

13. Interview Questions

  • Q: Explain the architectural difference between URL-based routing in React DOM (web) and Stack-based routing in React Native.
  • Q: What is the specific purpose of the react-native-safe-area-context package dependency within the React Navigation ecosystem?
  • Q: A developer has 50 screens in their application. Should all 50 screens be loaded into the memory array of the NavigationContainer simultaneously upon app launch? How does the Stack Navigator optimize RAM?

14. FAQs

Q: Can I remove the top white Header bar that says "Home"? A: Yes! You can pass options to the screen: <Stack.Screen name="Home" component={HomeScreen} options={{ headerShown: false }} />.

15. Summary

In Chapter 14, we established the foundational architecture for a multi-screen application. We bypassed the lack of a built-in router by installing the industry-standard React Navigation suite. We carefully installed native dependencies using Expo's version-matching tool, and wrapped our root application in the vital NavigationContainer. Finally, we implemented a Native Stack Navigator, successfully registering our independent screen components into the push/pop routing dictionary.

16. Next Chapter Recommendation

Our screens are registered in the dictionary, but we currently have no way to move from Home to Details. Proceed to Chapter 15: React Native Screens and Routing.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·