Navigation in React Native
# CHAPTER 14
Navigation in React Native
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-navigationcore and its native dependencies.
-
Understand the concept of the
NavigationContainer.
- Understand the "Stack" (Push/Pop) navigation metaphor.
-
Architect the root
App.jsfile 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:
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:
Step 3: Install the Stack Navigator We want standard "push to new screen" functionality, so we install the 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
HomeScreencard on the table.
-
When you tap a product, it Pushes the
DetailsScreencard *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
HomeScreencard 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.
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
9. Common Mistakes
-
Forgetting
NavigationContainer: If you try to use<Stack.Navigator>directly inApp.jswithout 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
HomeScreenandDetailsScreenin the sameApp.jsfile for demonstration. In a real application, you must create ascreens/folder, placeHomeScreen.jsin there, andimport HomeScreen from './screens/HomeScreen'intoApp.js. Keep your routing file clean!
11. Practice Exercises
- 1. What specific component acts as the root state manager for the entire routing system and must wrap all navigators?
- 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
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?
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-contextpackage 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
NavigationContainersimultaneously 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 vitalNavigationContainer. Finally, we implemented a Native Stack Navigator, successfully registering our independent screen components into the push/pop routing dictionary.