Lesson 46 of 60 – Navigation Parameters in React Native
77%

Navigation Parameters in React Native

Navigation parameters allow us to pass data from one screen to another screen in a React Native application. This is useful when the destination screen needs information about the item, user, product, course or record selected on the previous screen.

For example, when a user selects a course, we may pass the course ID to the Course Details screen.

Courses
   ↓
Select Course
   ↓
Course Details
   ↓
Course ID = 101
Note: Navigation parameters are commonly passed as the second argument of navigation.navigate() and accessed through the destination screen's route.params.

1. What are Navigation Parameters?

Navigation parameters are values passed from one screen to another during navigation.

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

Here, userId is a navigation parameter.

2. Why are Navigation Parameters Used?

Parameters are useful when the next screen needs information from the current screen.

  • User ID
  • Product ID
  • Course ID
  • Student information
  • Category information
  • Selected item
  • Search information

3. Basic Navigation Parameter Syntax

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

The first argument is the route name and the second argument contains the parameters.

4. Passing a String Parameter

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

The destination screen can receive the string value Rahul.

5. Passing a Number Parameter

navigation.navigate(
    'Details',
    {
        productId: 101
    }
);

Numbers can be used for IDs, quantities, prices and other numeric values.

6. Passing Multiple Parameters

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

Multiple related values can be passed together in the parameter object.

7. Accessing route.params

The destination screen receives a route object. Parameters can be accessed using route.params.

function ProfileScreen({
    route
}) {

    console.log(
        route.params
    );

}

8. Reading One Parameter

function ProfileScreen({
    route
}) {

    const name =
        route.params.name;

    return (

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

    );

}

The name parameter is read from route.params.

9. Destructuring route.params

Instead of repeatedly writing route.params, we can destructure the values.

function ProfileScreen({
    route
}) {

    const {
        userId,
        name
    } = route.params;

    return (

        <View>

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

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

        </View>

    );

}

10. Passing a Boolean Parameter

navigation.navigate(
    'Details',
    {
        isStudent: true
    }
);

Boolean values can be used when the destination screen needs a true/false value.

11. Passing an Array Parameter

navigation.navigate(
    'Courses',
    {
        courses: [
            'React Native',
            'JavaScript',
            'Python'
        ]
    }
);

An array can be passed when the destination screen needs a collection of values.

12. Passing an Object Parameter

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

Objects can contain several related properties.

13. Reading an Object Parameter

function ProductDetails({
    route
}) {

    const {
        product
    } = route.params;

    return (

        <View>

            <Text>
                {product.name}
            </Text>

            <Text>
                ₹{product.price}
            </Text>

        </View>

    );

}

14. Passing a Course ID

Course applications commonly pass a course ID to the details screen.

navigation.navigate(
    'CourseDetails',
    {
        courseId: 25
    }
);

The Course Details screen can use this ID to display the selected course.

15. Passing a Student ID

navigation.navigate(
    'StudentProfile',
    {
        studentId: 1001
    }
);

A student profile screen can receive the student ID and use it to load the appropriate student information.

16. Passing Parameters from a FlatList

<FlatList
    data={courses}
    keyExtractor={
        item => item.id.toString()
    }
    renderItem={({ item }) => (

        <Pressable
            onPress={() =>
                navigation.navigate(
                    'CourseDetails',
                    {
                        courseId: item.id
                    }
                )
            }
        >

            <Text>
                {item.name}
            </Text>

        </Pressable>

    )}
/>

This pattern is commonly used when selecting an item from a list.

17. Parameters with Pressable

<Pressable
    onPress={() =>
        navigation.navigate(
            'Profile',
            {
                userId: 101
            }
        )
    }
>

    <Text>
        Open Profile
    </Text>

</Pressable>

Pressable is useful for creating custom navigation buttons.

18. Optional Parameters

Sometimes a screen may receive a parameter only in certain situations. Before using it, the application should account for the possibility that it is unavailable.

const name =
    route.params?.name;

Optional chaining can prevent an error when the parameter object or property is missing.

19. Default Parameter Values

const {
    name = 'Guest'
} = route.params || {};

A default value can be used when the parameter is not available.

20. Updating Navigation Parameters

A screen may need to update its route parameters in certain navigation flows.

navigation.setParams({
    title: 'Updated Profile'
});

This can update parameters associated with the current route.

21. Passing Parameters with Stack Navigation

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

The destination route can receive parameters when navigating to this registered screen.

navigation.navigate(
    'Details',
    {
        productId: 101
    }
);

22. Passing Parameters Between Tabs

Navigation parameters can also be used when moving between screens managed by tab navigation.

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

The exact navigation structure determines how nested navigators should be addressed.

23. Passing Parameters in Drawer Navigation

A drawer-managed screen can also receive navigation parameters.

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

The Profile screen can read the value from its route parameters.

24. Using Parameters to Load API Data

A common real-world pattern is to pass an ID and use that ID to load data from an API.

useEffect(() => {

    const loadCourse = async () => {

        const response =
            await fetch(
                `https://example.com/api/courses/${courseId}`
            );

        const data =
            await response.json();

        setCourse(data);

    };

    loadCourse();

}, [courseId]);

Here, courseId can come from the navigation parameters.

25. Navigation Parameters and Detail Screens

Parameters are especially useful for detail screens.

Course List
     ↓
Course ID: 25
     ↓
Course Details
     ↓
Load Course 25

The list screen does not need to send all course information if the details screen can load the record using its ID.

26. Common Navigation Parameter Mistakes

  • Using the wrong route name.
  • Using the wrong parameter name.
  • Trying to access missing parameters without checking them.
  • Passing unnecessarily large objects.
  • Using inconsistent parameter names.
  • Assuming parameters always exist.
  • Forgetting to read parameters from the route object.
  • Passing sensitive information unnecessarily through navigation.

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

        </View>

    );

}

function StudentScreen({
    navigation,
    route
}) {

    const {
        studentId,
        name
    } = route.params;

    return (

        <View>

            <Text>
                Student Profile
            </Text>

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

            <Text>
                Name: {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="Student"
                    component={StudentScreen}
                />

            </Stack.Navigator>

        </NavigationContainer>

    );

}

28. Navigation Parameter Best Practices

  • Use meaningful parameter names.
  • Pass only the data required by the destination screen.
  • Prefer IDs when the destination can load the complete record.
  • Check optional parameters before using them.
  • Keep parameter structures simple.
  • Use consistent names throughout the application.
  • Avoid passing unnecessary sensitive information.
  • Document important navigation data flows in larger projects.

29. Important Navigation Parameter Concepts

Concept Purpose
navigation.navigate() Navigate to another screen and optionally pass parameters
route Provides information about the current route
route.params Contains parameters passed to the route
Parameter Object Stores values passed to the destination screen
studentId Example of an identifier parameter
productId Example of a product identifier
route.params?.name Safely accesses an optional parameter
navigation.setParams() Updates parameters for the current route

30. Complete Navigation Parameters Workflow

Select an Item
       ↓
Get Item ID
       ↓
Call navigation.navigate()
       ↓
Pass Parameter Object
       ↓
Open Destination Screen
       ↓
Read route.params
       ↓
Use Parameter
       ↓
Load Additional Data if Required
       ↓
Display Details
       ↓
Navigate Back

Navigation parameters are essential when screens need to exchange specific information. A common pattern is to pass an ID from a list screen to a details screen and then use that ID to load the required data.

📌 Key Points

  • Navigation parameters allow data to be passed between screens.
  • Parameters are commonly passed through navigation.navigate().
  • The destination screen can access them through route.params.
  • Strings, numbers, booleans, arrays and objects can be passed.
  • IDs are commonly passed to detail screens.
  • FlatList items can pass their IDs to detail screens.
  • Optional parameters should be handled safely.
  • navigation.setParams() can update route parameters.
  • Parameters can be used to load additional API data.
  • Only necessary data should be passed between screens.

🧠 Quick Quiz

Question: Where are navigation parameters commonly accessed in the destination screen?