我正在编写一个React原生应用程序,我想检测屏幕上任何地方的点击/点击,所以我在我的<View>元素上放了一个onClick处理程序,但它似乎不起作用.以下是我的渲染功能:

<View style={styles.container} onClick={this.onClick}>
    <Text style={styles.welcome}>
        Tap to change the background
    </Text>
</View>

我需要做什么?

推荐答案

要使任何元素在React原生UI中处理touch /点击事件,需要将该元素放在可touch 不透明、可touch 无反馈、可touch 原生反馈或可touch 高亮元素中:

<TouchableHighlight onPress = { this.onClick }>
    <View style={styles.container}>
    <Text style={styles.welcome}>
        Tap to change the background
    </Text>
    </View>
</TouchableHighlight>

希望有帮助.

React-native相关问答推荐

Appcenter构建失败错误号::EEXIST -文件存在于syserr_fail2_in

如何在使用嵌套的堆栈导航器在选项卡之间导航后弹出到堆栈顶部?

使用 Auth Token 标头react 本机图像,如何处理 401?

获取 TypeError:this.InnerNativeModule.configureProps 不是 mac 上 expo 中的函数

是否有用于react-native 的简单同步存储选项?

react native HTML entities

reactnavigation后退按钮的自定义后退导航

如何在 React Native 的 Modal 组件上禁用Swipe down to close?

Test suite无法运行 TypeError:Cannot read property ‘default’ of undefined

如何使用功能组件向 ref 公开功能?

自定义原生基础的选项卡

有react-native复选框的例子吗?

React Native:当我从 XCode 运行应用程序时看不到 console.logs

TypeError : props.navigation.getParam 不是函数. In(props.navigation.getParam('name')

React Native ScrollView 在 iOS 上从底部被截断

如何在样式组件之外获取主题?

由于对 sourcetree 的 husky 预推送,Git 推送失败

降级 react-native 的适当机制

在 React Native 中将props传递给外部样式表?

为什么 this.state 在react-native中未定义?