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) -->
期望结果:点击"点击我"后,应调用onChildClick()函数
问题:点击"点击我"后,却调用了onParentClick()函数。
当点击父元素时,得到的结果为"父元素被触发",并且完全正常。
但是当点击子元素时,得到的结果却仍然是"父元素被触发"。
我猜测onChildClick()函数没有被触发。
stopPropagation
是一个DOM方法。我不使用React Native,但从我所知道的来看,它仍然包含currentTarget和target,因此使用上面重复的React-carousel
最有可能是OP想要的。 - KeithpointerEvents='box-none'
属性。 - Olegdater