Navigation helps users move between different screens in an app easily. It controls what screen shows up next, making the app feel smooth and organized.
Why navigation manages screen flow in React Native
import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; const Stack = createStackNavigator(); function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
NavigationContainer wraps your whole app to manage navigation state.
Stack.Navigator defines the order and type of screens.
import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; const Stack = createStackNavigator(); function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> </Stack.Navigator> </NavigationContainer> ); }
function HomeScreen({ navigation }) {
return (
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
);
}This app has two screens: Home and Details. You start on Home. Pressing the button moves you to Details. On Details, pressing Go Back returns you to Home.
import React from 'react'; import { Button, Text, View } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Home Screen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Details')} /> </View> ); } function DetailsScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>Details Screen</Text> <Button title="Go Back" onPress={() => navigation.goBack()} /> </View> ); } const Stack = createStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Navigation keeps track of where you are in the app and what screen to show next.
Using navigation makes your app easier to use and understand.
Always wrap your app in NavigationContainer to enable navigation features.
Navigation controls the flow between screens in an app.
It helps users move forward and backward smoothly.
React Navigation uses containers and navigators to manage screens.
Practice
Navigation helps us to:Solution
Step 1: Understand the purpose of navigation
Navigation is designed to help users move between different screens or pages in an app.Step 2: Identify the correct function of navigation
Changing colors or storing data are not related to navigation's main role.Final Answer:
Move between different screens smoothly -> Option BQuick Check:
Navigation controls screen flow = C [OK]
- Confusing navigation with styling
- Thinking navigation stores data
- Assuming navigation changes fonts
Solution
Step 1: Recall React Navigation syntax
The official way to create a stack navigator is by calling createStackNavigator() function.Step 2: Check each option's syntax
Only const Stack = createStackNavigator(); matches the correct function call and syntax.Final Answer:
const Stack = createStackNavigator(); -> Option DQuick Check:
Correct function name = B [OK]
- Using 'new' keyword incorrectly
- Mixing function and object syntax
- Swapping words in function name
navigation.navigate('Profile');What happens when this line runs?
Solution
Step 1: Understand navigation.navigate()
This function moves the user to the named screen, here 'Profile'.Step 2: Analyze the options
Only The app moves to the Profile screen correctly describes moving to the Profile screen; others describe unrelated or error states.Final Answer:
The app moves to the Profile screen -> Option CQuick Check:
navigation.navigate('ScreenName') moves screens = A [OK]
- Thinking navigate reloads or closes app
- Confusing navigate with goBack
- Assuming navigate causes errors without context
function HomeScreen() {
return (
<Button title="Go" onPress={() => navigation.navigate('Details')} />
);
}Solution
Step 1: Check variable usage in HomeScreen
The code uses navigation.navigate but navigation is not passed or imported in the function.Step 2: Verify other options
Button supports onPress, navigate is correct method, and Title prop is case-sensitive but correct here.Final Answer:
navigation is not defined inside HomeScreen -> Option AQuick Check:
navigation must be defined or passed = A [OK]
- Forgetting to get navigation prop
- Misnaming navigate method
- Misunderstanding Button props
Solution
Step 1: Understand navigation.goBack()
This method moves the user back to the previous screen in the navigation stack.Step 2: Compare with other methods
reset clears history, push adds a new screen, replace swaps current screen; only goBack moves backward.Final Answer:
navigation.goBack() -> Option AQuick Check:
Go back to previous screen = goBack() = D [OK]
- Using reset to go back (it clears history)
- Confusing push with goBack
- Thinking replace goes back
Ready to prove it?
Pass the test and this pattern moves unknown
