Search code examples
react-nativereact-native-flatlist

How to display flatlist's selected item from one screen to another screen in react-native?


How to display flatlist's selected item from one screen to another screen in react-native ?

I am trying to select gender using flatlist .so when i select gender from flatlist at that time i want to show that selected gender value on next screen in react-native.so please help me .Thank you in advance.

How i can achieve this functionality.


Solution

  • In your scenario, you can simply pass the values using params in react-navigation - View

    Assuming that you can get flat list selected value ( Gender ) clearly :)

    Assuming that you have to pass values to SecondScreen

    This is function that get's selected item from FlatList

    itemOnClickListner = gender => {
      // gender is the selected gender of the user
    
      //Now you need to pass it to second screen
      this.props.navigation.navigate('SecondScreen', {
        screen: SecondScreen,
        Gender: gender,
      });
    
    };
    

    Inside SecondScreen ( The screen we are passing values ) state you need to save the passing value like this

      this.state = {
          SelectedGender: this.props.navigation.state.params.Gender,
        };