![](/img/trans.png)
[英]How to use conditional setState and give that value to another component in React Native?
[英]Select an item from dropdown and use that value in another component in React native
我只是想做这样的事情(当用户 select 一个项目,然后导航到另一个组件):
const handleValueChange=(itemValue, itemIndex) =>setTypeValue(itemValue)
const onPress = () => {
try{
const topic = "Plant/type";
...
navigation.navigate('Air')
}catch(err){
console.log(err)
}
}
return (
<Picker
selectedValue={typeValue}
onValueChange={handleValueChange}
style={{ top: '21%', height: 50, width: 150 }}/>
<TouchableOpacity
style={styles.button}
onPress={()=> onPress()}
/>
)
通常,当我们想在两个组件之间传递值时,我们会使用 props:
<AirScreen typeofPlant={typeValue} />
但在这种情况下,我不知道如何在不调用 AirScreen 的情况下做到这一点
只需执行以下操作:
navigation.navigate('RouteName', { /* params go here */ })
您可能需要阅读以下文档: https://reactnavigation.org/docs/params/
你可以这样做:
const onPress = () => {
try{
const topic = "Plant/type";
...
navigation.navigate('Air', {newTopic: topic}) //you can pass another value by separating with comma
}catch(err){
console.log(err)
}
}
然后您可以在下一个屏幕中获取相同的值,例如:
function NextScreen({ route, navigation }) {
const topic = route.params.newTopic
}
希望这对你有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.