Lesson 47 of 60 – Nested Navigation in React Native
78%

Nested Navigation in React Native

Nested Navigation means placing one navigator inside another navigator. It is useful when a React Native application has multiple levels of navigation.

For example, an application can have a Drawer Navigator containing Bottom Tabs, while one of the tabs contains its own Stack Navigator.

Drawer
   |
   └── Bottom Tabs
          |
          ├── Home Stack
          │      ├── Home
          │      └── Details
          │
          ├── Courses
          │
          └── Profile
Note: Nested navigation should be used when different parts of an application have their own navigation requirements. The navigation structure should remain as simple as possible.

1. What is Nested Navigation?

Nested navigation means placing one navigator inside another navigator.

For example:

Stack
  |
  └── Tabs
        |
        ├── Home
        ├── Courses
        └── Profile

Here, the Tab Navigator is nested inside the Stack Navigator.

2. Why Use Nested Navigation?

Large applications often have different navigation requirements for different sections.

Nested navigation can help organize these requirements.

  • Drawer containing tabs
  • Tabs containing stacks
  • Stack containing a tab navigator
  • Multiple navigation flows
  • Authentication and main application navigation

3. Simple Nested Navigation Structure

A simple example is a Stack Navigator containing a Tab Navigator.

Stack Navigator
      |
      └── Main Tabs
            |
            ├── Home
            ├── Courses
            └── Profile

The Stack can manage screens outside the tabs, while the tabs manage the main sections.

4. Stack Navigator with Tabs

A common application structure is to create a Stack Navigator and put the Tab Navigator inside one of its screens.

Stack
 |
 ├── Login
 |
 └── MainTabs
       |
       ├── Home
       ├── Courses
       └── Profile

This structure can be useful when a login screen should not display the main application tabs.

5. Creating a Tab Navigator

import {
    createBottomTabNavigator
} from '@react-navigation/bottom-tabs';

const Tab =
    createBottomTabNavigator();

The Tab Navigator can then contain the application's main sections.

6. Creating the Main Tabs

function MainTabs() {

    return (

        <Tab.Navigator>

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

            <Tab.Screen
                name="Courses"
                component={CoursesScreen}
            />

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

        </Tab.Navigator>

    );

}

The MainTabs component now contains three tabs.

7. Putting Tabs Inside a Stack

<Stack.Navigator>

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

</Stack.Navigator>

The Tab Navigator is now being used as a screen inside the Stack Navigator.

8. Adding a Login Screen

A login screen can be placed outside the main tabs.

<Stack.Navigator>

    <Stack.Screen
        name="Login"
        component={LoginScreen}
    />

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

</Stack.Navigator>

The login screen and the main application tabs now belong to the same stack.

9. Navigating from Login to Main Tabs

navigation.navigate(
    'MainTabs'
);

After successful login, the application can navigate to the main tab navigation.

10. Using replace() After Login

navigation.replace(
    'MainTabs'
);

Replacing the login route can be useful when the login screen should not remain in the normal back-navigation flow.

11. Drawer Containing Tabs

Another common nested structure is a Drawer Navigator containing a Tab Navigator.

Drawer
   |
   └── MainTabs
          |
          ├── Home
          ├── Courses
          └── Profile

The drawer can contain additional screens alongside the main tabs.

12. Creating MainTabs for a Drawer

function MainTabs() {

    return (

        <Tab.Navigator>

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

            <Tab.Screen
                name="Courses"
                component={CoursesScreen}
            />

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

        </Tab.Navigator>

    );

}

13. Putting MainTabs Inside Drawer

<Drawer.Navigator>

    <Drawer.Screen
        name="MainTabs"
        component={MainTabs}
        options={{
            headerShown: false
        }}
    />

    <Drawer.Screen
        name="Settings"
        component={SettingsScreen}
    />

</Drawer.Navigator>

The drawer now contains the tab-based main application and a separate Settings screen.

14. Tab Containing a Stack

A tab can also contain its own Stack Navigator.

Tabs
 |
 ├── Home Stack
 │      ├── Home
 │      └── Details
 │
 ├── Courses
 │
 └── Profile

This is useful when the Home section has several related screens.

15. Creating a Home Stack

function HomeStack() {

    return (

        <HomeStackNavigator>

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

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

        </HomeStackNavigator>

    );

}

In an actual implementation, the navigator object must be created with createNativeStackNavigator().

16. Registering a Stack as a Tab

<Tab.Screen
    name="Home"
    component={HomeStack}
/>

<Tab.Screen
    name="Courses"
    component={CoursesScreen}
/>

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

The Home tab now displays the Home Stack instead of a single screen.

17. Navigating Inside a Nested Stack

If the Home tab contains a stack, the Home screen can navigate to Details normally.

navigation.navigate(
    'Details'
);

The Details screen belongs to the Home Stack.

18. Navigating to a Nested Navigator

A parent navigator can navigate to a screen managed by a nested navigator using the appropriate route structure.

navigation.navigate(
    'MainTabs',
    {
        screen: 'Profile'
    }
);

Here, MainTabs is the parent route and Profile is a screen inside the nested navigator.

19. Passing Parameters to a Nested Screen

navigation.navigate(
    'MainTabs',
    {
        screen: 'Profile',
        params: {
            userId: 101
        }
    }
);

The nested Profile screen can receive the specified parameters.

20. Nested Navigation with Drawer and Stack

Drawer
   |
   ├── HomeStack
   │      ├── Home
   │      └── Details
   │
   ├── Courses
   │
   └── Settings

This structure allows the Home section to have multiple screens while the Drawer manages the main application sections.

21. Three-Level Nested Navigation

Large applications can have more than two navigation levels.

Drawer
   |
   └── Tabs
        |
        └── Home Stack
              |
              ├── Home
              ├── Details
              └── Checkout

This is a three-level navigation structure.

22. Nested Navigation and Back Button

Back navigation depends on the current navigation state and the navigator that manages the current screen.

Home
  ↓
Details
  ↓
Checkout

Pressing Back from Checkout normally returns to Details, and another Back action can return to Home.

23. Nested Navigation and Headers

Nested navigators can have their own headers. Sometimes the parent header should be hidden to avoid displaying multiple headers.

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

This allows the nested navigator to manage its own interface.

24. Nested Navigation for an Education App

Drawer
   |
   ├── Main Tabs
   │      ├── Home
   │      ├── Courses
   │      └── Profile
   │
   ├── Assignments
   │
   └── Settings

The Home and Courses sections can further contain their own stack navigation if needed.

25. Nested Navigation for a Shopping App

Tabs
 |
 ├── Home Stack
 │      ├── Products
 │      ├── Product Details
 │      └── Reviews
 │
 ├── Cart
 │
 └── Profile

The Home section has its own stack while Cart and Profile remain individual tab screens.

26. Common Nested Navigation Mistakes

  • Creating unnecessary levels of nesting.
  • Using confusing route names.
  • Not understanding which navigator owns a screen.
  • Using the wrong navigation route when targeting nested screens.
  • Passing parameters to the wrong level.
  • Showing multiple unnecessary headers.
  • Making back navigation difficult to understand.
  • Creating navigation structures that are harder to maintain than necessary.

27. Complete Stack and Tab Nested Example

import React from 'react';

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

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

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

import {
    createBottomTabNavigator
} from '@react-navigation/bottom-tabs';

const Stack =
    createNativeStackNavigator();

const Tab =
    createBottomTabNavigator();

function HomeScreen({
    navigation
}) {

    return (

        <View>

            <Text>
                Home Screen
            </Text>

        </View>

    );

}

function DetailsScreen() {

    return (

        <View>

            <Text>
                Details Screen
            </Text>

        </View>

    );

}

function CoursesScreen() {

    return (

        <View>

            <Text>
                Courses Screen
            </Text>

        </View>

    );

}

function ProfileScreen() {

    return (

        <View>

            <Text>
                Profile Screen
            </Text>

        </View>

    );

}

function HomeStack() {

    return (

        <Stack.Navigator>

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

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

        </Stack.Navigator>

    );

}

function MainTabs() {

    return (

        <Tab.Navigator>

            <Tab.Screen
                name="Home"
                component={HomeStack}
                options={{
                    headerShown: false
                }}
            />

            <Tab.Screen
                name="Courses"
                component={CoursesScreen}
            />

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

        </Tab.Navigator>

    );

}

export default function App() {

    return (

        <NavigationContainer>

            <Stack.Navigator>

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

            </Stack.Navigator>

        </NavigationContainer>

    );

}

28. Nested Navigation Best Practices

  • Use nesting only when it provides a clear navigation benefit.
  • Keep the navigation hierarchy easy to understand.
  • Give each navigator a clear responsibility.
  • Use meaningful route names.
  • Hide unnecessary parent headers.
  • Keep parameters associated with the correct navigator level.
  • Test forward and backward navigation carefully.
  • Avoid deeply nested navigation unless the application genuinely requires it.

29. Important Nested Navigation Concepts

Concept Purpose
Parent Navigator Contains another navigator or screens
Nested Navigator A navigator placed inside another navigator
Stack inside Tabs Provides detailed screen flows within a tab
Tabs inside Stack Provides main sections inside a stack flow
Drawer inside App Provides side-menu navigation
screen Selects a nested screen when navigating through a parent route
params Passes data to a nested destination
headerShown Controls whether a navigator screen header is displayed

30. Complete Nested Navigation Workflow

Plan Application Screens
          ↓
Choose Required Navigators
          ↓
Create Parent Navigator
          ↓
Create Nested Navigator
          ↓
Register Screens
          ↓
Connect Navigators
          ↓
Configure Headers
          ↓
Navigate Between Levels
          ↓
Pass Parameters When Required
          ↓
Test Back Navigation
          ↓
Keep the Navigation Tree Simple

Nested Navigation is an important concept for larger React Native applications. By combining Stack, Bottom Tab and Drawer navigators, developers can create organized navigation structures for real-world mobile applications.

📌 Key Points

  • Nested Navigation means placing one navigator inside another.
  • Stack navigators can contain tab navigators.
  • Tab navigators can contain stack navigators.
  • Drawer navigators can contain tabs or stacks.
  • Nested navigation is useful for larger applications.
  • screen can be used to target a screen inside a nested navigator.
  • params can be passed to nested destinations.
  • Parent headers can be hidden when necessary.
  • Each navigator should have a clear responsibility.
  • Avoid unnecessary or deeply complicated navigation structures.

🧠 Quick Quiz

Question: What does Nested Navigation mean in React Native?