React Native Pass Value From One Screen to Another Using React Navigation

React Native Pass Value From One Screen to Another Using React Navigation

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.

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

  1. Pass params to a route by putting them in an object as a second parameter to the navigation.navigate function from First Screen. Using :
  2. Read the params in your Second screen. Using:

This example is updated for the React Navigation 4.0+. For more about React Navigation 4 please have a look at Using React Navigation 4.0 in React Native apps.

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 the react-native-cli command line utility. Open the terminal and go to the workspace and run

npm install -g react-native-cli

Run the following commands to create a new React Native project

react-native init ProjectName

If you want to start a new project with a specific React Native version, you can use the --version argument:

react-native init ProjectName --version X.XX.X
react-native init ProjectName --version react-native@next

This will make a project structure with an index file named App.js in your project directory.

Installation of Dependencies

To switch between the screens we need to add react navigation in our application.

To install the dependencies open the terminal and jump into your project

1. Install react-navigation dependency to import createAppContainer

2. Install react-native-gesture-handler dependency to support gesture navigation

3. Install react-navigation-stack to import createStackNavigator

These commands will copy all the dependencies into your node_module directory.

CocoaPods Installation

After the updation of React Native 0.60, they have introduced autolinking so we do not require to link the libraries but need to install pods.

In this example we need to install the pods for react-native-gesture-handler.

Please use the following command to install CocoaPods

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

Open pages/FirstPage.js in any code editor and the Replace the code with the following code.

FirstPage.js

Open pages/SecondPage.js in any code editor and the Replace the code with the following code.

SecondPage.js

To Run the React Native App

Open the terminal again and jump into your project using.
cd ProjectName
To run the project on an Android Virtual Device or on real debugging device
react-native run-android
or on the iOS Simulator by running
react-native run-ios (macOS only).

     

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. 🙂

18 thoughts on “React Native Pass Value From One Screen to Another Using React Navigation”

  1. it was very useful. Now i want to know how to implement conditional operator in this. Once we entered value the value will be displayed in the next page.. now i want to know that if we didn’t enter the value an empty screen display in the second page so for that i want to show an invalid value instead of it.. can you help me out of this…

  2. this is fine if i want to pass the value from first screen to 3rd screen what should i do?th same method is not working

  3. It works perfectly. Now i want to add a button in the second page that goes back to the first page. already there is a back arrow in the second page but i need a button in second page that return to first page

    • You can not directly send the data to the 3rd and 4th screen you have to take the data in the middle screen and pass the same from there also. You can also use async storage to store the value and you can access those values any time.

  4. i wanna take value from 1 to 3 page.
    i am passing 1 to 2 and 2 to 3 its not working, how to do that using react native navigation

  5. Hi, when I am trying this approach, I get always default value not that one which I am sending from the first screen.
    1)First screen
    this.props.navigation.navigate(‘AnotherScreen’, { text: ‘some text’ })
    2)AnotherScreen
    this.props.navigation.getParam(‘text’, ‘defaultValue’)

    And when I am fallowed your way I got “undefined” from this.props.navigation.state.params.text

    My version of react navigation is : 4.0.1 .

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.