gpt4 book ai didi

javascript - 如何防止 native react 中的父点击事件?

转载 作者:行者123 更新时间:2023-11-29 20:48:27 27 4
gpt4 key购买 nike

onParentClick = () => {
console.log('Parent is triggered');
}

onChildClick = (event) => {
event.stopPropagation();
console.log('Child is triggered');
}
<TouchableWithoutFeedback onPress={()=> this.onParentClick()}>
<View>
<Text>How to prevent parent click event</Text>
<TouchableOpacity onPress={(event)=> this.onChildClick(event)}>
<Text> Click Me </Text>
</TouchableOpacity>
</View>
</TouchableWithoutFeedback>
<!-- edit description:- there was this end curly brace missing in above,
however the snippet will not run because the language js will not
support it and language html will not be able to format it correctly or run it.
(need to run the snippet on the react native environment like code-pen) -->

预期:点击“Click Me”时,必须调用 onChildClick()

问题:点击“Click Me”时,会调用 onParentClick()。

如何防止点击“点击我”时 parent 点击事件?

我点击父级得到的结果是“父级被触发”,它工作得很好。

但是在点击子项时,我得到的结果是“父项被触发”。

我猜 onChildClick() 没有被触发。

最佳答案

老问题,但我遇到了同样的问题,就我而言,问题是:

import {TouchableOpacity} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';

这个奇怪的导入是由于 eslint 的自动建议而发生的。不知何故,这些库不能很好地协同工作,所以我将上面的内容更改为:

import {TouchableOpacity, TouchableWithoutFeedback} from 'react-native';

关于javascript - 如何防止 native react 中的父点击事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53225643/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com