javascript - React Native - Callback setstate not work properly in iOS
问题描述
I have a modal component (loader) and used it almost on every screen, but the problem is when I've setState
and calling Alert
in callback setState
it's not work properly,
here's my code:
LoginProcess(){
this.setState({postLoader:true})
//calling API and handle if the password is wrong, showing alert
if(response.ok && response.status == 200){
//navigate to home
} else {
this.setState({postLoader:false},()=>Alert.alert("Perhatian", "Password salah!\nSilahkan coba kembali"))
}
}
<View>
<PostLoader showModal={this.state.postLoader} nameLoader="Mengirim data"/> //the loader using modal
</View>
as you can see in else{}
I've handle the callback
to show alert
, but the actual result is strange,
my postLoader
state is still true
and the Alert
popup and close itself, so i can't do anything except swipe my app in recent apps
and reopen,
anyone has an idea how to wait postLoader
state to false then call Alert
?
解决方案
您可以使用 setTimeout,它将在 this.setState 之后指定的 ms(毫秒)之后执行,并将根据需要工作。
LoginProcess(){
this.setState({postLoader:true})
//calling API and handle if the password is wrong, showing alert
if(response.ok && response.status == 200){
//navigate to home
} else {
this.setState({postLoader:false})
setTimeout(() => {
Alert.alert("Perhatian", "Password salah!\nSilahkan coba kembali")
}, 100);
}
}
<View>
<PostLoader showModal={this.state.postLoader} nameLoader="Mengirim data"/> //the loader using modal
</View>
推荐阅读
- mod-rewrite - apache2静默重定向未按预期工作
- sql - 是否指定 INSERT IGNORE... SELECT 是按顺序执行的?
- javascript - 如何从角色的频道中删除权限-discord.js?
- c# - Linq to SQL 返回列表对象
- python - 得分随着使用 PCA 的组件数量而增加
- java - 如何在 Java 中将泛型 pbject 作为方法参数传递?
- html - css transform translate 在y轴上不起作用
- java - 相机意图打开相机应用程序(仅第一次)
- javascript - js target.extend中的函数声明
- html - 当我收到 403 错误时,如何使用邮递员下载网页内容?