Lesson 42 of 60 – React Native Navigation Setup
70%

React Native Navigation Setup

Before using navigation in a React Native application, we need to install the required packages and configure the project correctly. This lesson explains the basic setup process for React Navigation.

We will create a simple navigation project with two screens: Home and Profile. After completing the setup, we will move between these screens using stack navigation.

Note: React Navigation uses different packages for different navigation types. The packages required can vary depending on the navigator and the React Native project setup.

1. What is Navigation Setup?

Navigation setup means preparing a React Native project so that different screens can communicate through a navigation system.

The basic process is:

Install Packages
       ↓
Create Navigation Container
       ↓
Create Navigator
       ↓
Create Screens
       ↓
Register Screens
       ↓
Navigate Between Screens

2. React Navigation Package

The main package for React Navigation is @react-navigation/native.

npm install @react-navigation/native

This package provides the core navigation functionality used by the React Navigation ecosystem.

3. Native Stack Navigator Package

For stack navigation, install the native stack package.

npm install @react-navigation/native-stack

The native stack navigator provides a stack-based navigation pattern for React Native applications.

4. Installing Required Dependencies

React Navigation also uses platform-related dependencies. The exact command depends on whether the project uses Expo or the React Native Community CLI.

For an Expo project, the recommended approach is to install dependencies using Expo's package installer where appropriate.

npx expo install react-native-screens
npx expo install react-native-safe-area-context

These packages are commonly used by React Navigation.

5. Creating a Navigation Folder

For larger applications, navigation code can be organized in a separate folder.

project/
│
├── App.js
│
├── screens/
│   ├── HomeScreen.js
│   └── ProfileScreen.js
│
└── navigation/
    └── AppNavigator.js

Keeping navigation code separate makes the project easier to manage.

6. Creating the Home Screen

Create a file named HomeScreen.js.

import React from 'react';

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

export default function HomeScreen() {

    return (

        <View>

            <Text>
                Home Screen
            </Text>

        </View>

    );

}

7. Creating the Profile Screen

Create another file named ProfileScreen.js.

import React from 'react';

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

export default function ProfileScreen() {

    return (

        <View>

            <Text>
                Profile Screen
            </Text>

        </View>

    );

}

8. Importing NavigationContainer

The NavigationContainer is imported from @react-navigation/native.

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

It contains the navigation structure of the application.

9. Importing Native Stack

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

This function creates a native stack navigator.

10. Creating the Stack Object

After importing createNativeStackNavigator, create a Stack object.

const Stack =
    createNativeStackNavigator();

The Stack object provides the Navigator and Screen components.

11. Creating Stack.Navigator

<Stack.Navigator>

</Stack.Navigator>

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

12. Creating Stack.Screen

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

The Stack.Screen registers a component as a navigation screen.

13. Registering Multiple Screens

<Stack.Navigator>

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

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

</Stack.Navigator>

Both Home and Profile are now registered in the navigation stack.

14. Understanding the name Property

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

The name property defines the route name.

This name is used when navigating to the screen.

navigation.navigate('Profile');

15. Understanding the component Property

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

The component property tells React Navigation which React component should be displayed for that route.

16. Creating AppNavigator

For better project organization, the navigation structure can be placed in AppNavigator.js.

import React from 'react';

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

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

import HomeScreen
    from '../screens/HomeScreen';

import ProfileScreen
    from '../screens/ProfileScreen';

const Stack =
    createNativeStackNavigator();

17. Completing AppNavigator

export default function AppNavigator() {

    return (

        <NavigationContainer>

            <Stack.Navigator>

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

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

            </Stack.Navigator>

        </NavigationContainer>

    );

}

This component contains the complete navigation structure.

18. Using AppNavigator in App.js

The navigation component can be imported into the main App.js file.

import React from 'react';

import AppNavigator
    from './navigation/AppNavigator';

export default function App() {

    return (
        <AppNavigator />
    );

}

Now App.js uses the navigation structure defined in the navigation folder.

19. Navigating from Home to Profile

The Home screen receives a navigation object.

export default function HomeScreen({
    navigation
}) {

    return (

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

    );

}

Pressing the button opens the Profile screen.

20. Going Back to the Previous Screen

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

navigation.goBack();

For example:

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

21. Setting a Screen Title

The screen header can be customized using the options property.

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

The navigation header will use the specified title.

22. Hiding the Header

The header can be hidden for a particular screen.

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

This can be useful when creating a custom full-screen UI.

23. Passing Parameters During Navigation

Navigation parameters can be passed while opening another screen.

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

The destination screen can access these values through its route object.

24. Reading Navigation Parameters

export default function ProfileScreen({
    route
}) {

    const {
        userId,
        name
    } = route.params;

    return (

        <View>

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

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

        </View>

    );

}

25. Checking the Navigation Project

After completing the setup, test the application.

App
 ↓
NavigationContainer
 ↓
Stack.Navigator
 ↓
Home Screen
 ↓
Profile Screen

Check that the Home screen appears first and that the Profile screen can be opened successfully.

26. Common Setup Errors

  • Installing the wrong navigation package.
  • Forgetting to install the native stack package.
  • Missing required platform dependencies.
  • Using incorrect import paths.
  • Forgetting NavigationContainer.
  • Using an unregistered route name.
  • Using an incorrect screen component.
  • Not restarting the development server when required after configuration changes.

27. Complete Navigation Setup 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 Profile"
                onPress={() =>
                    navigation.navigate(
                        'Profile',
                        {
                            name: 'Rahul'
                        }
                    )
                }
            />

        </View>

    );

}

function ProfileScreen({
    navigation,
    route
}) {

    return (

        <View>

            <Text>
                Profile Screen
            </Text>

            <Text>
                Name: {route.params.name}
            </Text>

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

        </View>

    );

}

export default function App() {

    return (

        <NavigationContainer>

            <Stack.Navigator>

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

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

            </Stack.Navigator>

        </NavigationContainer>

    );

}

28. Navigation Setup Best Practices

  • Keep screen components in a dedicated screens folder.
  • Keep complex navigation configuration organized.
  • Use meaningful route names.
  • Choose the navigator according to the application structure.
  • Keep parameters simple and relevant.
  • Test navigation on the target platforms.
  • Keep navigation code separate when the application becomes large.
  • Avoid unnecessary nested navigators.

29. Important Navigation Setup Components

Component / Package Purpose
@react-navigation/native Core React Navigation functionality
@react-navigation/native-stack Native stack navigation
NavigationContainer Contains the navigation tree
createNativeStackNavigator() Creates a native stack navigator
Stack.Navigator Contains stack screens
Stack.Screen Registers a screen
navigation.navigate() Moves to another screen
navigation.goBack() Returns to the previous screen

30. Complete Navigation Setup Workflow

Create React Native Project
          ↓
Install React Navigation
          ↓
Install Required Navigator
          ↓
Install Required Dependencies
          ↓
Create Screens
          ↓
Create NavigationContainer
          ↓
Create Navigator
          ↓
Register Screens
          ↓
Set Screen Options
          ↓
Navigate Between Screens
          ↓
Pass Parameters
          ↓
Test Back Navigation

Once the navigation setup is complete, you can build applications with multiple screens. The next step is to learn individual navigation patterns such as stack navigation in more detail.

📌 Key Points

  • React Navigation provides navigation functionality for React Native applications.
  • @react-navigation/native provides the core navigation package.
  • @react-navigation/native-stack provides native stack navigation.
  • NavigationContainer contains the navigation structure.
  • Stack.Navigator manages stack-based screens.
  • Stack.Screen registers individual screens.
  • Route names should be clear and consistent.
  • navigation.navigate() moves to another screen.
  • navigation.goBack() returns to the previous screen.
  • Parameters can be passed between screens.
  • Navigation code can be organized in a dedicated folder.

🧠 Quick Quiz

Question: Which component is used to contain the navigation structure in a React Navigation application?