React Native Pass TextInput Entered Value From One Activity Screen to Another

Transforming data between activities is one of the most useful task in react native applications because using them we can communicate between activities by passing values to one another. The React Navigation library has its own method known as this.props.navigation.navigate{} which is used to navigate to next activity and with that it can also pass value from one activity screen to  another.  So in this tutorial we would going to Pass TextInput Entered Value From One Activity Screen to Another Activity using this.props.navigation.navigate{} method of React Navigation. So let’s get started .

Contents in this project Pass TextInput Entered Value From One Activity Screen to Another Activity in Android iOS React Native App Example Tutorial:

1. Before getting started with coding part we need to install the React Navigation library in our current react native project in order to use the Activity structure in our react native app. So to install the react navigation library open your project’s folder in CMD (Terminal) and run the below command .

Screenshot of CMD:


Screenshot after successfully install the React Navigation library:

2. Import StyleSheet, TextInput, View, TouchableOpacity and Text component in your project.

3. Import StackNavigator module from react navigation library in your project. This is used to add multiple activities in your project.

4. Create a Class named as MainActivity, this would be our first home screen of application.

5. Create static navigationOptions in your Activity to set Activity screen Title.

6. Creating Constructor() in your MainActivity class and inside the constructor we would make 2 States named as TextInput_Name and TextInput_Number . These states would used to hold the Text Input entered values.

7. Create a function named as Send_Data_Function() in MainActivity class. Inside this function we would use the this.props.navigation.navigate() method to navigate to next activity class and with that we are also sending the Values of States.

Second : Here the second means the SecondActivity. This will redirect us the second activity.

NameOBJ : Used as a Key object that holds the value of Name state . We would use this object into next activity for receiving name.

NumberOBJ : Used as key object that holds the value of Number state. We would use this object into another activity to receive the sent value of number.

8. Creating 2 Text Input and 1 button component in MainActivity class inside render’s return block. We are using the TouchableOpacity component to make the button.

onChangeText : Used when user starts typing inside Text Input component and save the typed data into State.

9. Complete code of MainActivity class.

10. Creating another class named as SecondActivity. Inside this class we would receive the transfer value from previous activity and show inside the Text component. You can do whatever you want to using this value.

this.props.navigation.state.params.NameOBJ : As you can see the NameOBJ is the key which we have created in our previous MainActivity. So we would use this method to receive the value .

this.props.navigation.state.params.NumberOBJ : Used to receive the value of number object.

Complete source code of SecondActivity :

11. Creating the StackNavigator activity navigator block. Inside this block we would define our all the activities and give them a unique identifier name which is used on navigation time.

12. Creating Style.

13. Complete source code for App.js File :

Screenshots:

Pass TextInput Entered Value From One Activity Screen to Another