另一个TouchableOpacity内的Stacked TouchableOpacity无法点击

mor*_*fer 13 android react-native touchableopacity

即使本文档(https://facebook.github.io/react-native/docs/gesture-responder-system.html)指出,触摸事件也会传递给子节点,并且仅由父节点使用,如果孩子对事件没有反应,我面对的问题是,嵌套在另一个TouchableOpacity内的TouchableOpacity对触摸没有正确反应.

我的结构如下

<ScrollView>
  <TouchableOpacity onPress={() => console.log('This is printed always')}>
    <View>
      <Text>I can click here</Text>
      <TouchableOpacity onPress={() => console.log('This is printed never')}>
        <Text>I can click here but the outer onPress is called instead of the inner one</text>
      </TouchableOpacity>
    </View>
  </TouchableOpacity>
</ScrollView>
Run Code Online (Sandbox Code Playgroud)

TouchableOpacitys中的按钮也是如此:单击按钮调用父TouchableOpacity的onPress方法

我在监督什么吗?

Man*_*vin 27

更改可触摸不透明度的导入

import { TouchableOpacity } from 'react-native-gesture-handler';
Run Code Online (Sandbox Code Playgroud)

到下面,现在一切都会好起来的!

import { TouchableOpacity } from 'react-native';
Run Code Online (Sandbox Code Playgroud)

  • 我从“react-native”导入了相同的内容,但它不起作用。 (3认同)
  • 我必须在项目中添加“react-native-gesture-handler”依赖项,因为它是“@react-navigation/stack”的对等依赖项。这给自动导入带来了痛苦,因为“TouchableOpacity”现在有两个依赖项...... (2认同)

小智 7

您可以只使用TouchableWithoutFeedback包装内部TouchableOpacity

就像是:

<TouchableOpacity onPress={() => console.log('This is printed always')}>
    <View>
        <Text>I can click here</Text>
        <TouchableWithoutFeedback>
            <TouchableOpacity onPress={() => console.log('This is printed never')}>
                <Text>I can click here but the outer onPress is called instead of the inner one</text>
            </TouchableOpacity>
        </TouchableWithoutFeedback>
    </View>
</TouchableOpacity>
Run Code Online (Sandbox Code Playgroud)


小智 6

写在这里是为了让它更加突出。

正如 @EliezerSteinbock 提到的,嵌套可能是TouchableOpacity从与父级不同的东西导入的。这很有可能,因为我们许多人在可视化代码或其他 IDE 上使用自动导入。

遗憾的是我第一次错过了她的评论,所以希望这对其他人有帮助

  • 答对了!...该死的`react-native-gesture-handler`:-) (2认同)