In this section, we will learn how to pass parameters to routes in React Navigation. This is a crucial feature for creating dynamic and interactive applications where different screens need to communicate with each other.
Key Concepts
- Navigation Parameters: Data passed between screens.
- Route Parameters: Parameters specific to a route.
- Navigation Prop: A prop automatically passed to each screen component, containing methods and properties for navigation.
Passing Parameters
- Passing Parameters to a Route
To pass parameters to a route, you use the navigate method provided by the navigation prop. Here’s a basic example:
// HomeScreen.js
import React from 'react';
import { Button, View, Text } from 'react-native';
const HomeScreen = ({ navigation }) => {
return (
<View>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details', { itemId: 86, otherParam: 'anything you want here' })}
/>
</View>
);
};
export default HomeScreen;In this example, when the button is pressed, the navigate method is called with the name of the route ('Details') and an object containing the parameters ({ itemId: 86, otherParam: 'anything you want here' }).
- Accessing Parameters in the Route
To access the parameters in the target route, you use the route prop. Here’s how you can do it:
// DetailsScreen.js
import React from 'react';
import { View, Text } from 'react-native';
const DetailsScreen = ({ route, navigation }) => {
// Get the params from the route
const { itemId, otherParam } = route.params;
return (
<View>
<Text>Details Screen</Text>
<Text>Item ID: {itemId}</Text>
<Text>Other Param: {otherParam}</Text>
</View>
);
};
export default DetailsScreen;In this example, route.params contains the parameters passed from the HomeScreen.
Practical Example
Let’s create a simple app with two screens: HomeScreen and DetailsScreen. We will pass parameters from HomeScreen to DetailsScreen.
Step-by-Step Implementation
-
Install React Navigation:
npm install @react-navigation/native npm install @react-navigation/stack npm install react-native-screens react-native-safe-area-context -
Set Up Navigation:
// App.js import * as React from 'react'; import { NavigationContainer } from '@react-navigation/native'; import { createStackNavigator } from '@react-navigation/stack'; import HomeScreen from './HomeScreen'; import DetailsScreen from './DetailsScreen'; const Stack = createStackNavigator(); const App = () => { return ( <NavigationContainer> <Stack.Navigator initialRouteName="Home"> <Stack.Screen name="Home" component={HomeScreen} /> <Stack.Screen name="Details" component={DetailsScreen} /> </Stack.Navigator> </NavigationContainer> ); }; export default App; -
Create HomeScreen:
// HomeScreen.js import React from 'react'; import { Button, View, Text } from 'react-native'; const HomeScreen = ({ navigation }) => { return ( <View> <Text>Home Screen</Text> <Button title="Go to Details" onPress={() => navigation.navigate('Details', { itemId: 86, otherParam: 'anything you want here' })} /> </View> ); }; export default HomeScreen; -
Create DetailsScreen:
// DetailsScreen.js import React from 'react'; import { View, Text } from 'react-native'; const DetailsScreen = ({ route, navigation }) => { const { itemId, otherParam } = route.params; return ( <View> <Text>Details Screen</Text> <Text>Item ID: {itemId}</Text> <Text>Other Param: {otherParam}</Text> </View> ); }; export default DetailsScreen;
Exercises
Exercise 1: Passing Additional Parameters
Modify the HomeScreen to pass an additional parameter called description to the DetailsScreen. Display this parameter in the DetailsScreen.
Solution:
// HomeScreen.js
import React from 'react';
import { Button, View, Text } from 'react-native';
const HomeScreen = ({ navigation }) => {
return (
<View>
<Text>Home Screen</Text>
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details', { itemId: 86, otherParam: 'anything you want here', description: 'This is a description' })}
/>
</View>
);
};
export default HomeScreen;
// DetailsScreen.js
import React from 'react';
import { View, Text } from 'react-native';
const DetailsScreen = ({ route, navigation }) => {
const { itemId, otherParam, description } = route.params;
return (
<View>
<Text>Details Screen</Text>
<Text>Item ID: {itemId}</Text>
<Text>Other Param: {otherParam}</Text>
<Text>Description: {description}</Text>
</View>
);
};
export default DetailsScreen;Exercise 2: Navigating Back with Parameters
Modify the DetailsScreen to include a button that navigates back to the HomeScreen and passes a parameter called message with the value "Returned from Details".
Solution:
// DetailsScreen.js
import React from 'react';
import { View, Text, Button } from 'react-native';
const DetailsScreen = ({ route, navigation }) => {
const { itemId, otherParam, description } = route.params;
return (
<View>
<Text>Details Screen</Text>
<Text>Item ID: {itemId}</Text>
<Text>Other Param: {otherParam}</Text>
<Text>Description: {description}</Text>
<Button
title="Go Back"
onPress={() => navigation.navigate('Home', { message: 'Returned from Details' })}
/>
</View>
);
};
export default DetailsScreen;
// HomeScreen.js
import React from 'react';
import { Button, View, Text } from 'react-native';
const HomeScreen = ({ navigation, route }) => {
const message = route.params?.message;
return (
<View>
<Text>Home Screen</Text>
{message && <Text>{message}</Text>}
<Button
title="Go to Details"
onPress={() => navigation.navigate('Details', { itemId: 86, otherParam: 'anything you want here', description: 'This is a description' })}
/>
</View>
);
};
export default HomeScreen;Conclusion
In this section, we learned how to pass parameters between routes in React Navigation. We covered the basics of passing and accessing parameters and implemented a practical example to solidify our understanding. This knowledge is essential for creating dynamic and interactive applications where different screens need to communicate with each other. In the next module, we will explore networking and data handling in React Native.
React Native Course
Module 1: Introduction to React Native
- What is React Native?
- Setting Up the Development Environment
- Hello World App
- Understanding JSX
- Components and Props
Module 2: Core Components and Styling
- Core Components Overview
- Text, View, and Image
- Styling with Flexbox
- Handling User Input
- ScrollView and ListView
Module 3: State and Lifecycle
- State and Lifecycle Methods
- Handling Events
- Conditional Rendering
- Lists and Keys
- Forms and Controlled Components
Module 4: Navigation
- Introduction to React Navigation
- Stack Navigator
- Tab Navigator
- Drawer Navigator
- Passing Parameters to Routes
Module 5: Networking and Data
- Fetching Data with Fetch API
- Using Axios for HTTP Requests
- Handling Network Errors
- AsyncStorage for Local Data
- Integrating with REST APIs
Module 6: Advanced Concepts
Module 7: Deployment and Publishing
- Building for iOS
- Building for Android
- Publishing to App Store
- Publishing to Google Play
- Continuous Integration and Delivery
