Passing parameters between screens lets you send information from one screen to another. This helps screens show different content based on what you send.
Passing parameters between screens in React Native
navigation.navigate('ScreenName', { paramName: paramValue }) // To read the parameter in the target screen: const { paramName } = route.params;
Use navigation.navigate to move to another screen and send parameters as an object.
In the receiving screen, use route.params to access the parameters.
navigation.navigate('Profile', { userId: 42 })
const { userId } = route.params;
console.log(userId);This app has two screens: Home and Details. When you press the button on Home, it sends a message to Details. Details shows that message on screen.
import React from 'react'; import { Button, Text, View } from 'react-native'; import { NavigationContainer } from '@react-navigation/native'; import { createNativeStackNavigator } from '@react-navigation/native-stack'; const Stack = createNativeStackNavigator(); function HomeScreen({ navigation }) { return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Button title="Go to Details" onPress={() => navigation.navigate('Details', { message: 'Hello from Home!' })} /> </View> ); } function DetailsScreen({ route }) { const { message } = route.params; return ( <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}> <Text>{message}</Text> </View> ); } export default function App() { return ( <NavigationContainer> <Stack.Navigator> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }
Always check if route.params exists before using it to avoid errors.
Parameters can be any data type: strings, numbers, objects, or arrays.
Use descriptive parameter names to keep your code clear.
Use navigation.navigate('Screen', { params }) to send data between screens.
Access sent data in the target screen with route.params.
This helps screens show different content based on passed information.
Practice
userId with value 42 when navigating to a screen called Profile in React Native?Solution
Step 1: Understand navigation method
To pass parameters, usenavigation.navigatewith the screen name and an object of params.Step 2: Check parameter syntax
The parameter must be inside an object like{ userId: 42 }as the second argument.Final Answer:
navigation.navigate('Profile', { userId: 42 }) -> Option DQuick Check:
Pass params with navigate(screen, { params }) [OK]
- Using wrong method like goTo or move
- Passing params outside an object
- Forgetting to include params argument
itemId inside a React Native screen component?Solution
Step 1: Identify how params are accessed
In React Navigation, the screen receives arouteprop containingparams.Step 2: Use correct property chain
Access the parameter withroute.params.itemId.Final Answer:
const itemId = route.params.itemId; -> Option CQuick Check:
Use route.params to get passed parameters [OK]
- Trying to get params from navigation instead of route
- Incorrect property order like navigation.route.params
- Using props.navigation.params which is invalid
navigation.navigate('Details', { productId: 101 });
// In Details screen component:
const Details = ({ route }) => {
return {route.params.productId};
};
What will be displayed on the screen?Solution
Step 1: Understand parameter passing
TheproductIdwith value101is passed to the Details screen vianavigation.navigate.Step 2: Check how parameter is used in Details
The component accessesroute.params.productIdand displays it inside aTextcomponent.Final Answer:
101 -> Option BQuick Check:
Passed param value is displayed [OK]
- Expecting param name string instead of value
- Using undefined or wrong property to access param
- Not passing params correctly in navigate
name to a screen called Welcome?
navigation.navigate('Welcome', name: 'Alice');Solution
Step 1: Check navigate method syntax
The second argument tonavigatemust be an object containing parameters.Step 2: Identify error in parameter passing
The code passesname: 'Alice'without wrapping it in curly braces, which is invalid syntax.Final Answer:
Parameters must be passed inside an object as the second argument. -> Option AQuick Check:
Params go inside an object in navigate [OK]
- Passing params without curly braces
- Using wrong syntax for navigate arguments
- Assuming screen names must be lowercase
Home screen to a Profile screen and pass both userId and userName. How should you write the navigation call to pass these parameters correctly?Solution
Step 1: Understand parameter object structure
Multiple parameters must be passed inside a single object with key-value pairs.Step 2: Check each option's syntax
navigation.navigate('Profile', { userId: 5, userName: 'John' }) correctly passes bothuserIdanduserNameinside one object.Final Answer:
navigation.navigate('Profile', { userId: 5, userName: 'John' }) -> Option AQuick Check:
Pass multiple params inside one object [OK]
- Passing params as separate arguments
- Using array instead of object for params
- Splitting params into multiple objects
Ready to prove it?
Pass the test and this pattern moves unknown
