React Native Alert自动隐藏和取消显示



我在react native中遇到Alert问题。

我正在使用警报向用户显示消息。在安卓系统中,警报工作正常,但在iOS中,警报会自动弹出并消失。

代码:

export const loginUser = (values) => {
return (dispatch) => {
console.log("REDUX THUNK STARTED !");
console.log("GET DATA FROM : ", Helpers.USER_LOGIN);
dispatch(onLoginRequest(true));
fetch(Helpers.USER_LOGIN, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
email: values.userEmail,
password: values.userPassword,
}),
})
.then((res) => {
dispatch(onLoginRequest(false));
console.log("Response Status from the URL - ", res.status);
console.log("REDUX THUNK COMPLETE !");
//Handle the response accordingly
if (res.status == "200") {
values.navigation.navigate("HomeScreen");
dispatch(onLoginSuccess(true));
} else if (res.status == "400") {
//Invalid credentails
Alert.alert(
"Error",
"Please Check Email and Password",
[{ text: "OK", onPress: () => dispatch(onLoginSuccess(false)) }],
{ cancelable: false }
);
} else {
errorDialog.globalError();
}
})
.catch((e) => {
console.warn("URL Fetch Error - ", e);
dispatch(onLoginRequest(false));
dispatch(onLoginFailure(true));
console.log("REDUX THUNK ERROR !");
errorDialog.globalError();
});
};
};

我找到了解决方案。问题是我的加载微调器覆盖库

它将自动关闭Alert

作为一个解决方案,我为警报添加了setTimeout功能

代码

setTimeout(() => {
Alert.alert(
'Error',
'Please Check Email and Password',
[{text: 'OK', onPress: () => dispatch(onLoginSuccess(false))}],
{cancelable: false},
);
}, 100);

您需要添加异步等待或承诺

else if (res.status == "400") {
showAlert();
}
function showAlert(){
return new Promise((resolve, reject) => {
Alert.alert(
"Error",
"Please Check Email and Password",
[
{text: 'Ok', onPress: () => {resolve('ok') },
],
{cancelable: false},
);
});
}

最新更新