Lesson 43 of 60 – Stack Navigation in React Native
72%

Stack Navigation in React Native

Stack Navigation is one of the most commonly used navigation patterns in React Native applications. It allows screens to be placed in a stack and lets users move forward to a new screen and return to the previous screen.

For example, a shopping application may use stack navigation like this:

Home
  ↓
Product Details
  ↓
Cart
  ↓
Checkout
Note: In this lesson, we will use the native stack navigator from React Navigation to create and manage multiple screens.

1. What is Stack Navigation?

Stack navigation manages screens in a stack-like structure. When a new screen is opened, it is placed on top of the previous screen.

Home
  ↓
Details
  ↓
Profile

The Profile screen is currently at the top of the stack.

2. How Does a Stack Work?

A stack follows a Last In, First Out concept.

Open Home

Home
────

Open Details

Details
Home
────

Open Profile

Profile
Details
Home
────

When the user goes back, the top screen is removed from the stack.

3. Installing Native Stack

The native stack navigator can be installed using:

npm install @react-navigation/native-stack

The core React Navigation package should also be installed in the project.

npm install @react-navigation/native

4. Importing NavigationContainer

import {
    NavigationContainer
} from '@react-navigation/native';

The NavigationContainer contains the navigation tree of the application.

5. Importing createNativeStackNavigator

import {
    createNativeStackNavigator
} from '@react-navigation/native-stack';

This function is used to create a native stack navigator.

6. Creating the Stack

const Stack =
    createNativeStackNavigator();

The Stack object provides components such as Stack.Navigator and Stack.Screen.

7. Creating Stack.Navigator

<Stack.Navigator>

    {/* Screens */}

</Stack.Navigator>

The Stack.Navigator contains all screens that belong to the stack.

8. Creating Stack.Screen

<Stack.Screen
    name="Home"
    component={HomeScreen}
/>

Stack.Screen registers a React component as a route in the stack.

9. Creating Multiple Stack Screens

<Stack.Navigator>

    <Stack.Screen
        name="Home"
        component={HomeScreen}
    />

    <Stack.Screen
        name="Details"
        component={DetailsScreen}
    />

    <Stack.Screen
        name="Profile"
        component={ProfileScreen}
    />

</Stack.Navigator>

Three screens are now registered in the stack.

10. Initial Screen

The first registered screen is normally displayed when the navigation stack starts.

<Stack.Screen
    name="Home"
    component={HomeScreen}
/>

The Home screen can therefore act as the initial screen of the application.

11. Navigating to a Screen

The navigate() method is used to move to another registered screen.

navigation.navigate(
    'Details'
);

The route name must match a registered Stack.Screen name.

12. Navigation with Button

<Button
    title="Open Details"
    onPress={() =>
        navigation.navigate(
            'Details'
        )
    }
/>

Pressing the button pushes the Details screen into the navigation flow.

13. Navigation with Pressable

<Pressable
    onPress={() =>
        navigation.navigate(
            'Details'
        )
    }
>

    <Text>
        Open Details
    </Text>

</Pressable>

Pressable can be used to create a custom navigation button.

14. Going Back

The goBack() method returns the user to the previous screen.

navigation.goBack();

For example, if the current screen is Details and the previous screen is Home, calling goBack() returns to Home.

15. Stack Header

Native stack screens can display a header at the top of the screen.

<Stack.Screen
    name="Home"
    component={HomeScreen}
    options={{
        title: 'Home'
    }}
/>

The title can be customized using the options property.

16. Customizing Header Title

<Stack.Screen
    name="Profile"
    component={ProfileScreen}
    options={{
        title: 'My Profile'
    }}
/>

The navigation header will display the specified title.

17. Hiding the Header

<Stack.Screen
    name="Login"
    component={LoginScreen}
    options={{
        headerShown: false
    }}
/>

The header can be hidden for screens such as login or splash screens.

18. Passing Parameters

Data can be passed to another screen while navigating.

navigation.navigate(
    'Profile',
    {
        userId: 101,
        name: 'Rahul'
    }
);

The Profile screen can read these values using the route object.

19. Reading Parameters

function ProfileScreen({
    route
}) {

    const {
        userId,
        name
    } = route.params;

    return (

        <View>

            <Text>
                ID: {userId}
            </Text>

            <Text>
                Name: {name}
            </Text>

        </View>

    );

}

20. Passing an Object as Parameters

navigation.navigate(
    'Product',
    {
        product: {
            id: 10,
            name: 'Laptop',
            price: 50000
        }
    }
);

The destination screen can access the object through route.params.

21. Setting Initial Route

The initialRouteName option can be used to specify the initial route.

<Stack.Navigator
    initialRouteName="Home"
>

    <Stack.Screen
        name="Home"
        component={HomeScreen}
    />

    <Stack.Screen
        name="Profile"
        component={ProfileScreen}
    />

</Stack.Navigator>

This makes the initial screen explicit.

22. Replacing the Current Screen

In some application flows, you may want to replace the current route instead of simply navigating forward.

navigation.replace(
    'Home'
);

For example, after a successful login, an application may replace the login screen with the main application screen.

23. Resetting Navigation State

Navigation state can also be reset when an application needs to create a new navigation history.

navigation.reset({
    index: 0,
    routes: [
        {
            name: 'Home'
        }
    ]
});

This can be useful for flows where previous screens should no longer be available through normal back navigation.

24. Stack Navigation with Login

A common application flow is:

Login
  ↓
Home
  ↓
Profile
  ↓
Settings

After successful authentication, the application can move the user to the main application screens.

25. Stack Navigation with Product Details

A shopping application can use stack navigation like this:

Products
   ↓
Product Details
   ↓
Cart
   ↓
Checkout

Product information can be passed as navigation parameters when opening the Product Details screen.

26. Common Stack Navigation Mistakes

  • Forgetting to install the required navigation packages.
  • Forgetting NavigationContainer.
  • Using an unregistered route name.
  • Using incorrect screen component imports.
  • Passing incorrect parameter names.
  • Using too many unnecessary nested navigators.
  • Forgetting to handle loading or authentication flows properly.
  • Not testing back navigation.

27. Complete Stack Navigation Example

import React from 'react';

import {
    View,
    Text,
    Button
} from 'react-native';

import {
    NavigationContainer
} from '@react-navigation/native';

import {
    createNativeStackNavigator
} from '@react-navigation/native-stack';

const Stack =
    createNativeStackNavigator();

function HomeScreen({
    navigation
}) {

    return (

        <View>

            <Text>
                Home Screen
            </Text>

            <Button
                title="Open Details"
                onPress={() =>
                    navigation.navigate(
                        'Details',
                        {
                            productId: 101
                        }
                    )
                }
            />

        </View>

    );

}

function DetailsScreen({
    navigation,
    route
}) {

    return (

        <View>

            <Text>
                Product Details
            </Text>

            <Text>
                Product ID:
                {route.params.productId}
            </Text>

            <Button
                title="Go Back"
                onPress={() =>
                    navigation.goBack()
                }
            />

        </View>

    );

}

export default function App() {

    return (

        <NavigationContainer>

            <Stack.Navigator
                initialRouteName="Home"
            >

                <Stack.Screen
                    name="Home"
                    component={HomeScreen}
                    options={{
                        title: 'Home'
                    }}
                />

                <Stack.Screen
                    name="Details"
                    component={DetailsScreen}
                    options={{
                        title: 'Product Details'
                    }}
                />

            </Stack.Navigator>

        </NavigationContainer>

    );

}

28. Stack Navigation Best Practices

  • Use meaningful screen names.
  • Keep screen components separate.
  • Use a clear navigation hierarchy.
  • Pass only required parameters.
  • Use goBack() when appropriate.
  • Use replace() for flows where the previous screen should not remain in history.
  • Use initialRouteName when the starting route needs to be explicit.
  • Keep navigation configuration organized.
  • Test navigation and back behavior on real devices or emulators.

29. Important Stack Navigation Methods

Method / Property Purpose
navigate() Navigate to another route
goBack() Return to the previous screen
replace() Replace the current route
reset() Reset the navigation state
initialRouteName Specify the initial route
Stack.Navigator Defines the stack navigator
Stack.Screen Registers a screen
options Configures screen and header options

30. Complete Stack Navigation Workflow

Create React Native Project
          ↓
Install React Navigation
          ↓
Install Native Stack
          ↓
Create NavigationContainer
          ↓
Create Stack Navigator
          ↓
Create Screens
          ↓
Register Screens
          ↓
Set Initial Route
          ↓
Navigate to Another Screen
          ↓
Pass Parameters
          ↓
Use goBack()
          ↓
Use replace() When Required
          ↓
Test Navigation Flow

Stack navigation provides a simple and powerful way to manage screen-to-screen workflows in React Native applications. It is especially useful for login flows, product details, forms, profile pages and other sequential screen flows.

📌 Key Points

  • Stack navigation manages screens in a stack.
  • New screens are placed on top of the navigation stack.
  • createNativeStackNavigator() creates a native stack navigator.
  • Stack.Navigator contains the stack screens.
  • Stack.Screen registers individual screens.
  • navigation.navigate() moves to another screen.
  • navigation.goBack() returns to the previous screen.
  • navigation.replace() replaces the current route.
  • navigation.reset() can reset navigation state.
  • Parameters can be passed between screens.
  • initialRouteName can define the starting screen.
  • Stack navigation is useful for many real-world mobile application flows.

🧠 Quick Quiz

Question: Which method is commonly used to return to the previous screen in Stack Navigation?