问题
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