← Back to Chapters

Stack Navigation

? Stack Navigation

? Quick Overview

Stack Navigation is the most common navigation pattern in React Native apps. It allows screens to be placed on top of each other like a stack, enabling push and pop based navigation.

? Key Concepts

  • Screen stack behavior (push & pop)
  • Header and back button handling
  • Passing parameters between screens
  • Navigation lifecycle

? Syntax / Theory

Stack navigation is implemented using @react-navigation/native-stack. Each screen is registered inside a stack navigator.

? View Code Example
// Creating a stack navigator
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

const Stack = createNativeStackNavigator();

? Code Example

? View Code Example
// Basic stack navigation example
import React from 'react';
import { Text, Button, View } from 'react-native';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';

function HomeScreen({ navigation }) {
  return (
    <View>
      <Text>Home Screen</Text>
      <Button title="Go to Details" onPress={() => navigation.navigate('Details')} />
    </View>
  );
}

function DetailsScreen() {
  return (
    <View>
      <Text>Details Screen</Text>
    </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>
  );
}

? Live Output / Explanation

When the app launches, the Home screen appears first. Pressing the button pushes the Details screen onto the stack, showing a back button automatically.

? Interactive / Visual Understanding

The stack works like browser history: new screens are pushed, and the back button pops the current screen. Use the simulator below to understand how screens stack on top of each other.

9:41 100% ?
Home

? Home

Stack: [ Home ]
? Notice how new screens slide in from the right and cover the previous one.

? Use Cases

  • Login → Dashboard flow
  • List → Details screens
  • Settings navigation
  • Authentication stacks

✅ Tips & Best Practices

  • Keep stack depth minimal
  • Use meaningful screen names
  • Pass only required parameters
  • Separate stacks for major flows

? Try It Yourself

  • Add a third screen to the stack
  • Pass parameters between screens
  • Customize the header title
  • Disable header for a screen