繁体   English   中英

如何在 onPress 中使用 react-native 中的两个事件?

[英]how can i use two event in onPress with react-native?

我想使用 UploadB function 和 setModalVisible(.modalVisible)...

我试过这个,但它不起作用

    const UploadB = useCallback(() => {
      dispatch({
        type: ADD_POST_REQUEST,
        info:{foodname, ingre1, ingre2, ingre3, recipe},
      }),
      navigation.navigate('Main');
    }, [foodname, ingre1, ingre2, ingre3, recipe]);


         <Pressable
          style={[styles.button, styles.buttonClose]}
          onPress={ UploadB, () => setModalVisible(!modalVisible) }
        >

我该如何修复我的代码?

您只能将一个 function 分配给onPress 您始终可以创建一个 function 来满足您的一切需求,包括调用 UploadB 和切换模式可见性。

const press = useCallback(() => {
  UploadB()
  setModalVisible(visible => !visible)
}, [UploadB]);

<Pressable
  style={[styles.button, styles.buttonClose]}
  onPress={press}
/>

To remove modalVisibile as a dependency of press, or any state setter to depend on the state itself, you can pass function to the state setter ( setModalVisible ) that will take the current value as its first argument and return new value.

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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