react-native TouchableNativeFeedback onPress 不起作用

Shu*_*ood 1 react-native react-native-android touchableopacity react-navigation touchablewithoutfeedback

我创建了一个组合组件来将 TouchableNativeFeedback 组合到 wrapperComponent。

export default function withFeedback2(
    WrappedComponent
) {
    return class extends BaseComponent {
        constructor(props) {
            super(props);
        }

        render() {
            return (
                <View>
                    <TouchableNativeFeedback
                        onPress={() => this.props.onContainerViewPress()}

                    >
                        <WrappedComponent {...this.props} />
                    </TouchableNativeFeedback>
                    {/* <TouchableOpacity
                        onPress={this.props.onContainerViewPress ? () => this.props.onContainerViewPress() : null}

 >
                        <WrappedComponent {...this.props} />
                    </TouchableOpacity> */}
                </View>
            );
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

但是TochableNativeFeedback 的 OnPress事件没有触发。而 OnPress 事件被正确触发,并且如果 wrappercomponent 包装在 TouchableOpacity 下,则调用 wrappercomponent 的onContainerViewPress道具。

我正在 Android 平台上对此进行测试。

小智 11

使用 a<View></View>来包装您的WrappedComponentfor TouchableNativeFeedback

<TouchableNativeFeedback
  onPress={() => this.props.onContainerViewPress()}>
    <View>
      <WrappedComponent {...this.props} />
    </View>
</TouchableNativeFeedback>
Run Code Online (Sandbox Code Playgroud)

  • 我有类似的问题。我按照您的建议进行操作,并将我的组件包装在视图中,该视图由 TouchableNativeFeedback 包装。我没有得到连锁反应,但 onPress 被触发了。你能告诉我为什么会这样吗? (2认同)

man*_*gei 5

有两个不同的TouchableNativeFeedback类。确保您导入正确的:

import { TouchableNativeFeedback } from "react-native"
import { TouchableNativeFeedback } from "react-native-gesture-handler"
Run Code Online (Sandbox Code Playgroud)

我遇到了类似的问题,最终从“react-native”库中使用了它。从“react-native-gesture-handler”导入它对我不起作用。