Passing the value from one screen to another is one of the most useful things while we have an application which needs to communicate between the different screens. We can easily pass the data between different activities easily using the this.props.navigation.navigate() which is used to navigate between different screens. In the same method, we can easily pass the value and can get it from other screens.
This example will work for the React Navigation Version 3+. If you are using Version 2+ then please find the comments in the code for the suitable changes or can see our old post React Native Navigation Version 2+ for the help.
In our example below, we will pass an input taken from the first screen to the second screen.
To pass the value between different activities
- Pass params to a route by putting them in an object as a second parameter to the
navigation.navigate
function from First Screen. Using :1this.props.navigation.navigate('RouteName', { /* params go here */ }) - Read the params in your Second screen. Using:1this.props.navigation.getParam(paramName, defaultValue)
To Make a React Native App
Getting started with React Native will help you to know more about the way you can make a React Native project. We are going to use react-native init to make our React Native App. Assuming that you have node installed, you can use npm to install thereact-native-cli
command line utility. Open the terminal and go to the workspace and runInstallation of Dependency
To use createStackNavigator
you need to install react-navigation
package. To install this
Open the terminal and jump into your project
1 | cd ProjectName |
Run the following command
1 | npm install react-navigation --save |
This command will copy all the dependencies into your node_module directory, You can find the directory in node_module
the directory named react-navigation
.
–save is optional, it is just to update the react-navigation dependancy in your package.json file.
Dependency Version for this Example used is
1 | "react-navigation": "3.0.9" |
Project File Structure
To start with this Example you need to create a directory named pages in your project and create two files FirstPge.js and SecondPage.js in it.
Now open App.js in any code editor and replace the code with the following code.
App.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | //This is an example code to pass values between two screens using React Navigator// import React, { Component } from 'react'; //import react in our code. //In Version 2+ //import {createStackNavigator} from 'react-navigation'; //In Version 3+ import {createStackNavigator,createAppContainer} from 'react-navigation'; //import Navigator in our project import FirstPage from './pages/FirstPage'; import SecondPage from './pages/SecondPage'; //import all the screens we are going to switch const App = createStackNavigator({ //Constant which holds all the screens like index of any book FirstPage: { screen: FirstPage }, //First entry by default be our first screen //if we do not define initialRouteName SecondPage: { screen: SecondPage }, }, { initialRouteName: 'FirstPage', } ); //In version 2+ createAppContainer was default container //but in version 3+ you have to export it manually //For Version 2+ //export default App; //For Version 3+ export default createAppContainer(App); |
Open pages/FirstPage.js in any code editor and the Replace the code with the following code.
FirstPage.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | // First screen which we will use to send the data import React, { Component } from 'react'; //import react in our code. import { StyleSheet, View, Button, TextInput } from 'react-native'; //import all the components we are going to use. export default class FirstPage extends Component { constructor(props) { //constructor to set default state super(props); this.state = { username: '', }; } static navigationOptions = { //Setting the header of the screen title: 'First Page', }; render() { const { navigate } = this.props.navigation; return ( //View to hold our multiple components <View style={styles.container}> {/*Input to get the value from the user*/} <TextInput value={this.state.username} onChangeText={username => this.setState({ username })} placeholder={'Enter Any value'} style={styles.input} /> {/*Button to go to the next activity*/} <Button title="Go Next" //Button Title onPress={() => navigate('SecondPage', { JSON_ListView_Clicked_Item: this.state.username, }) } //On click of the button we will send //the data as a Json from here to the Second Screen using navigation prop /> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', alignItems: 'center', padding: 16, }, input: { width: 200, height: 44, padding: 10, marginBottom: 10, backgroundColor: '#DBDBD6', }, }); |
Open pages/SecondPage.js in any code editor and the Replace the code with the following code.
SecondPage.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | // Second screen which we will use to get the data import React, { Component } from 'react'; //import react in our code. import { StyleSheet, View, Text } from 'react-native'; //import all the components we are going to use. export default class SecondPage extends Component { static navigationOptions = { //Setting the header of the screen title: 'Second Page', }; render() { const { navigate } = this.props.navigation; return ( //View to hold our multiple components <View style={styles.container}> <Text> You are on SecondPage and the value passed from the first screen is </Text> {/*Using the navigation prop we can get the value passed from the first Screen*/} <Text style={styles.TextStyle}> {this.props.navigation.state.params.JSON_ListView_Clicked_Item} </Text> <Text style={{ marginTop: 16 }}>With Check</Text> {/*If you want to check the value is passed or not, you can use conditional operator.*/} <Text style={styles.TextStyle}> {this.props.navigation.state.params.JSON_ListView_Clicked_Item ? this.props.navigation.state.params.JSON_ListView_Clicked_Item : 'No Value Passed'} </Text> </View> ); } } const styles = StyleSheet.create({ container: { flex: 1, backgroundColor: '#fff', margin: 50, alignItems: 'center', justifyContent: 'center', }, TextStyle: { fontSize: 23, textAlign: 'center', color: '#f00', }, }); |
To Run the React Native App
Open the terminal again and jump into your project using.This is how you can pass the value from one screen to another screen. If you have any doubt or you want to share something about the topic you can comment below or contact us here. There will be more posts coming soon. Stay tuned!
Hope you liked it. 🙂
How useful was this post?
Click on a star to rate us!
Average rating / 5. Vote count:
We are sorry that this post was not useful for you!
Let us improve this post!
Thanks for your feedback!