admin管理员组

文章数量:1388822

I am trying to use the Alert ponent in React Native to create a consistent experience between Android and iOS. I am trying to run the example alert. I import the Alert ponent (omitted other imports for brevity):

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
  • I am seeing this error for Android and iOS

I am stumped as to how to fix this. Has anyone else had any luck getting this to work on version 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 ponents and methods and none of them are working.

Update 2

It is worth noting that the standard alert() function works without crashing. 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".

      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."

I am trying to use the Alert ponent in React Native to create a consistent experience between Android and iOS. I am trying to run the example alert. I import the Alert ponent (omitted other imports for brevity):

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
  • I am seeing this error for Android and iOS

I am stumped as to how to fix this. Has anyone else had any luck getting this to work on version 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 ponents and methods and none of them are working.

Update 2

It is worth noting that the standard alert() function works without crashing. 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".

      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."

Share Improve this question edited May 23, 2017 at 11:54 CommunityBot 11 silver badge asked Jan 25, 2017 at 15:36 Adam JakielaAdam Jakiela 2,2687 gold badges31 silver badges49 bronze badges 3
  • Can you provide more context where you are using the Alert. For example also include code of ponent and function where you are calling alert – coder hacker Commented Jan 25, 2017 at 15:48
  • @coderhacker Please see my update. – Adam Jakiela Commented Jan 25, 2017 at 16:07
  • Well you're definitely importing it and using it correctly...I would try restarting the packager and sim. If that doesn't work, maybe try doing react-native upgrade to check whether you're template files are up to date with your npm package. – Matt Aft Commented Jan 25, 2017 at 16:32
Add a ment  | 

1 Answer 1

Reset to default 2

I would try creating a separate function that calls for the Alert.alert('foo'). Then in your TouchableHighlight, use onPress={this.onYourFunctionName.bind(this)}

本文标签: javascriptAlertalert() not working in React Native 0361Stack Overflow