【问题标题】:Value of props variable in react-native navigationreact-native 导航中 props 变量的值
【发布时间】:2021-01-28 20:03:28
【问题描述】:

当我第一次按下按钮时,导航到具有变量 props.userGuid 值为 0000000 的另一个屏幕。然后从另一个屏幕导航.goBack()。然后我将变量 userGuid 的 setState 更改为 12345678,当再次按下功能组件中的按钮时,屏幕会加载旧的 0000000 值。为什么在 console.log 中显示新值时导航没有得到新值?

const DetailsScreenButton = (props) => {
        return (<TouchableOpacity
             onPress={() => {
                console.log(props.userGuid)   <---------- here I have value 1234567 while second press
              
                navigation.navigate('DietDetails', {
                    userGuid: props.userGuid  <---------- here I have allways value 0000000 
                });
            }}
        >
            <Text>Show details</Text>
        </TouchableOpacity>)
    }



return (
    <Modal
        animationType="fade"
        transparent={true}
        visible={modalDetailsVisible}
    >
        <View style={styles.centeredView}>
            <View style={styles.modalView}>
               <DetailsScreenButton userGuid={userGuid} />
            </View>
        </View>
    </Modal>
)


【问题讨论】:

    标签: reactjs react-native


    【解决方案1】:

    您正在更改状态,但没有更新道具。换句话说,您没有在 props 中发送新状态。

    请在更改状态的地方添加代码。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-09-23
      • 1970-01-01
      • 2022-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多