react-native - React Native async await dispatch store
问题描述
So I want to make a login feature, in here server will validate first if the username or password is correct or not. I'm using store, react - redux.
Here is my code when login button pressed
const [statusLogin,setStatusLogin] = useState(null)
let loginInfo = []
function loginButton(){
(async () => {
loginInfo = {username:username,password:password}
const { status } = await dispatch(getUser(loginInfo))
if (status==1){
console.log(status,'in status if 1')
setStatusLogin('granted')
}else{
console.log(status,'in status if else')
setStatusLogin(null)
}
})();
}
Here is my store that suppose to return value 1 or else if it returned value 1 geb statusLogin will changed as granted
export function getUser(body){
return dispatch =>{
if (!body){
setTimeout(() => {
console.log('no username/pass')
}, 2000);
}else{
setTimeout(() => {
console.log('username/pass validated returning with value 1')
}, 2000);
}
}
}
help me please
解决方案
这可能会有所帮助
...
function loginButton() {
(async () => {
loginInfo = { username: username, password: password };
await dispatch(getUser(loginInfo, callback));
})();
}
function callback = (status) => {
if (status == 1) {
console.log(status, "in status if 1");
setStatusLogin("granted");
} else {
console.log(status, "in status if else");
setStatusLogin(null);
}
};
减速器.js
export function getUser(body, callback){
return dispatch =>{
if (!body){
setTimeout(() => {
console.log('no username/pass');
callback(0);
}, 2000);
}else{
setTimeout(() => {
console.log('username/pass validated returning with value 1')
callback(1);
}, 2000);
}
}
}
推荐阅读
- javascript - javascript SyntaxError:标识符“img”已被声明
- node.js - 使用 Firebase Admin Auth 检索用户数据的费用
- c# - 如果我尝试打开 PWM 引脚,Windows 10 Iot Core 应用程序会崩溃
- ruby-on-rails - Rails 活动记录数组计数为 0,但数据库中存在记录?
- python - 即使形状存在,也缺少形状参考
- google-maps - 是否可以将 3D 模型上传到 Google 地图?
- c64 - “?Out of data error in 60”在我的 C64 mini 上基本
- python - 使用 python 从网络画布中抓取数据
- asp.net - 网络配置上的“位置路径”无法识别的元素错误
- struct - 如何循环查询数据并插入结构?