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
1 Answer
Reset to default 2I 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
版权声明:本文标题:javascript - Alert.alert() not working in React Native 0.36.1 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744543281a2611742.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论