React Native Send 2 values on onPress event then pass to another screen

我只是一个虾纸丫 提交于 2019-12-24 10:56:16

问题


This is my JSON file

{
    "name": "Thịt bò",
    "noed": 5
},
{
    "name": "Thịt heo",
    "noed": 3
}

I get them to Flatlist

   <FlatList
      data={cats}
      keyExtractor={item => item.name}
      renderItem={({item})=>(
          <View>
              <Text style={styles.catsItem} onPress={() => this.changeTextInput(item.name)}>{item.name} {item.noed}</Text>
          </View>
      )}
/>

But I want to send 2 values are item.name and item.noed to TextInput then send them to another screen

changeTextInput(item){
        this.setState({name: item});
    };

But I don't know how to send item.noed to TextInput and how to send them to another screen. I'm quite new, so please help me.


回答1:


Use react-navigation npm package to redirect from one screen to another screen you can also pass values

There are two pieces to this:

Pass params to a route by putting them in an object as a second parameter to the navigation.navigate function:

this.props.navigation.navigate
('RouteName', { /* params go here */ })




onPress={()=>this.props.navigation.navigate('Details', 
    { itemId: 86, otherParam: 'anything you want here', })};

Read the params in your screen component:

this.props.navigation.getParam(paramName, defaultValue)

For more information please read the following document

React Navigation Link



来源:https://stackoverflow.com/questions/55226091/react-native-send-2-values-on-onpress-event-then-pass-to-another-screen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!