繁体   English   中英

Select 下拉列表中的一个项目,并在 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM