Keywords: React Native | React Navigation | StackNavigator | Parameter Passing
Abstract: This article explains how to pass parameters between screens in React Native using React Navigation's StackNavigator. It covers the correct methods for passing and accessing parameters, with code examples and best practices to avoid common pitfalls.
Introduction
In React Native development, navigating between screens often requires passing data. A common issue is how to correctly pass parameters when using StackNavigator from React Navigation. This article provides a comprehensive guide based on common problems and solutions.
Passing Parameters to a Screen
To pass parameters to a screen, use the second parameter of the navigate function. For instance, when navigating from a home screen to a details screen, you can pass the user object as follows:
onPress(user) {
this.props.navigation.navigate('DetailsPage', { user });
}
This method ensures that the user data is available in the target screen.
Accessing Parameters in the Target Screen
In React Navigation versions 5.x and above, parameters are accessed via the route.params object. For example, in the DetailsPage component:
render() {
const { user } = this.props.route.params;
return (
<View>
<Text>Name: {user.name}</Text>
<Text>Email: {user.email}</Text>
</View>
);
}
For older versions (4.x and below), use this.props.navigation.state.params instead.
Complete Code Example
Here is a revised version of the code from the query, incorporating the correct parameter passing:
// HomeScreen component
import React from 'react';
import { ListView, Text, TouchableHighlight, View } from 'react-native';
class HomeScreen extends React.Component {
// ... other code ...
onPress(user) {
this.props.navigation.navigate('DetailsPage', { user });
}
renderRow(user) {
return (
<TouchableHighlight onPress={() => this.onPress(user)}>
<View style={styles.row}>
<Text style={styles.rowText}>{user.name}</Text>
</View>
</TouchableHighlight>
);
}
// ... render method ...
}
// DetailsPage component
import React from 'react';
import { Text, View } from 'react-native';
export default class DetailsPage extends React.Component {
static navigationOptions = ({ navigation }) => ({
title: `User: ${navigation.state.params.user.name}`,
});
render() {
const { user } = this.props.route.params; // For React Navigation 5.x+
return (
<View>
<Text>Name: {user.name}</Text>
<Text>Email: {user.email}</Text>
</View>
);
}
}
Best Practices for Parameter Passing
It is advisable to pass only essential data, such as object IDs, rather than full objects. This minimizes issues with data consistency and deep linking. For example, instead of passing the entire user object, pass the user ID and fetch the data in the target screen.
// Better approach
onPress(user) {
this.props.navigation.navigate('DetailsPage', { userId: user.id });
}
Then, in DetailsPage, use the userId to retrieve user data from a global store or API.
Conclusion
By correctly implementing parameter passing in StackNavigator, developers can create more efficient and maintainable React Native applications. Always refer to the official React Navigation documentation for the latest practices.