A Stack Navigator helps you move between screens in an app, like pages in a book. It keeps track of where you are and lets you go back easily.
Stack Navigator in React Native
import { createNativeStackNavigator } from '@react-navigation/native-stack'; import { NavigationContainer } from '@react-navigation/native'; const Stack = createNativeStackNavigator(); function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Use NavigationContainer to wrap your navigator. It manages navigation state.
Each Stack.Screen defines a screen with a name and a component to show.
const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator>
<Stack.Screen name="Home" component={HomeScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}const Stack = createNativeStackNavigator();
function App() {
return (
<NavigationContainer>
<Stack.Navigator initialRouteName="Details">
<Stack.Screen name="Home" component={HomeScreen} />
<Stack.Screen name="Details" component={DetailsScreen} />
</Stack.Navigator>
</NavigationContainer>
);
}function HomeScreen({ navigation }) {
return (
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details')}
/>
);
}This app has two screens: Home and Details. From Home, you can go to Details. From Details, you can go back to Home.
import React from 'react'; import { Button, Text, View } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>Home Screen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Details')} /> </View> ); } function DetailsScreen({ navigation }) { return ( <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}> <Text>Details Screen</Text> <Button title="Go back" onPress={() => navigation.goBack()} /> </View> ); } const Stack = createNativeStackNavigator(); export default function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
The stack navigator keeps screens in a stack, so you can go forward and back easily.
Navigation actions like navigate and goBack control screen changes.
Stack navigation animations and header bars are handled automatically.
Stack Navigator manages screen navigation like a stack of pages.
Use it to move forward to new screens and back to previous ones.
Wrap your navigator in NavigationContainer and define screens inside Stack.Navigator.
Practice
Stack Navigator in React Native?Solution
Step 1: Understand navigation concepts
Stack Navigator organizes screens so you can move forward and back like a stack of pages.Step 2: Identify the correct purpose
It is not for styling, fetching data, or handling input but for navigation.Final Answer:
To manage screen navigation like a stack of pages -> Option CQuick Check:
Stack Navigator = navigation management [OK]
- Confusing navigation with styling
- Thinking it handles data fetching
- Assuming it manages user input
Solution
Step 1: Recognize correct nesting
TheNavigationContainermust wrap the entire navigator, which containsStack.Navigatorand its screens.Step 2: Check each option's structure
Only <NavigationContainer><Stack.Navigator><Stack.Screen name="Home" component={HomeScreen} /></Stack.Navigator></NavigationContainer> correctly wrapsStack.NavigatorinsideNavigationContainerand includesStack.Screen.Final Answer:
<NavigationContainer><Stack.Navigator><Stack.Screen name="Home" component={HomeScreen} /></Stack.Navigator></NavigationContainer> -> Option AQuick Check:
NavigationContainer wraps Stack.Navigator [OK]
- Placing Stack.Screen outside Stack.Navigator
- Wrapping Stack.Screen directly in NavigationContainer
- Incorrect nesting order of components
const Stack = createStackNavigator();
function App() {
return (
);
}Solution
Step 1: Identify default initial screen
By default, the firstStack.Screenlisted is the initial screen shown.Step 2: Check the order of screens
The first screen is named "Profile", so the app showsProfileScreenfirst.Final Answer:
Profile screen -> Option AQuick Check:
First Stack.Screen = initial screen [OK]
- Assuming Settings is first because of alphabetical order
- Thinking Home screen appears without definition
- Believing an error occurs without initial screen prop
const Stack = createStackNavigator();
function App() {
return (
);
}Solution
Step 1: Check for NavigationContainer
React Navigation requiresNavigationContainerto wrap the navigator for proper navigation context.Step 2: Verify other parts
Screen names can be uppercase, components can be custom, and one screen is allowed.Final Answer:
Missing NavigationContainer wrapping the navigator -> Option BQuick Check:
NavigationContainer is required [OK]
- Omitting NavigationContainer causes navigation failure
- Thinking screen names must be lowercase
- Assuming only built-in components can be screens
function HomeScreen({ navigation }) {
return (
???}
/>
);
}Solution
Step 1: Understand navigation methods
navigation.navigate('ScreenName')moves to a screen, pushing it on the stack if needed.Step 2: Compare options
push(Details)has a syntax error (missing quotes around screen name);goBackandpopToTopare for going back, not forward.Final Answer:
navigation.navigate('Details') -> Option DQuick Check:
Use navigate() to go forward to a screen [OK]
- Using goBack to go forward
- Using popToTop incorrectly
- Confusing push and navigate methods
Ready to prove it?
Pass the test and this pattern moves unknown
