简体   繁体   English

Alert.alert() 在 React Native 0.36.1 中不起作用

[英]Alert.alert() not working in React Native 0.36.1

I am trying to use the Alert component in React Native to create a consistent experience between Android and iOS.我正在尝试使用 React Native 中的Alert组件在 Android 和 iOS 之间创建一致的体验。 I am trying to run the example alert.我正在尝试运行示例警报。 I import the Alert component (omitted other imports for brevity):我导入 Alert 组件(为简洁起见省略了其他导入):

import {
  Alert, 
} from 'react-native';

I then create the alert provided in the example:然后我创建示例中提供的警报:

Alert.alert(
  'Alert Title',
  'My Alert Msg',
  [
    {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
    {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
    {text: 'OK', onPress: () => console.log('OK Pressed')},
  ]
)

However, I get the following error:但是,我收到以下错误:

在此处输入图片说明

I have found this post with a similar issue , however, I believe my problem is different because:我发现这篇文章有类似的问题,但是,我相信我的问题是不同的,因为:

  • I am running React Native version 0.36.1我正在运行 React Native 版本 0.36.1
  • I am seeing this error for Android and iOS我在 Android 和 iOS 上看到此错误

I am stumped as to how to fix this.我很难过如何解决这个问题。 Has anyone else had any luck getting this to work on version 0.36?有没有其他人有幸让它在 0.36 版上运行?

Update更新

As requested, here is an example of where I am trying to use the alert:根据要求,以下是我尝试使用警报的示例:

    <TouchableHighlight style={styles.button} underlayColor='transparent'   onPress={() => Alert.alert(
        'Alert Title',
        'Alert Message'
      )}>

This is just one instance where the code fails.这只是代码失败的一种情况。 I have tried multiple alerts across several components and methods and none of them are working.我在多个组件和方法中尝试了多个警报,但没有一个正常工作。

Update 2更新 2

It is worth noting that the standard alert() function works without crashing.值得注意的是,标准的alert()函数可以正常工作而不会崩溃。 However, I am not able to specify the title of the alert.但是,我无法指定警报的标题。 For instance the code below would return an alert with a title "Alert" and the message as "Please enter a 4 digit code".例如,下面的代码将返回标题为“警报”的警报和“请输入 4 位代码”的消息。

      alert("Invalid Code", "Please enter a 4 digit code.")

My desired output would be to have the title = "Invalid Code" and the message = "Please enter a 4 digit code."我想要的输出是标题 =“无效代码”和消息 =“请输入 4 位代码。”

I would try creating a separate function that calls for the Alert.alert('foo').我会尝试创建一个单独的函数来调用 Alert.alert('foo')。 Then in your TouchableHighlight, use onPress={this.onYourFunctionName.bind(this)}然后在你的onPress={this.onYourFunctionName.bind(this)} ,使用onPress={this.onYourFunctionName.bind(this)}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM