React native Alert 是否可以等待用户的响应(就像暂停应用程序一样)而不是弹出并继续执行以下逻辑?
我认为 js alert 只会暂停应用程序。
Tar*_*sri 12
以下是您可以执行的操作Alert:
您可以使用 false 设置可取消,因此用户无法在不按下按钮的情况下关闭警报
您可以为每个按钮设置回调。
你也可以用 Promise 包装 Alert,这样你就可以使用 async
const AsyncAlert = () => {
return new Promise((resolve, reject) => {
Alert.alert(
'Title',
'Message',
[
{text: 'YES', onPress: () => resolve('YES') },
{text: 'NO', onPress: () => resolve('NO') }
],
{ cancelable: false }
)
})
}
// Then to use the method
const userResponse = await AsyncAlert()
// ...the rest of your code
Run Code Online (Sandbox Code Playgroud)
小智 5
我已经获取了 @Tareq El-Masri 代码并对其进行了编辑。将其更改为异步函数就可以了。
const AsyncAlert = (title, msg) => new Promise((resolve) => {
Alert.alert(
title,
msg,
[
{
text: 'ok',
onPress: () => {
resolve('YES');
},
},
],
{ cancelable: false },
);
});
await AsyncAlert();Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5910 次 |
| 最近记录: |