Reactjs 函数中的可观察状态未更新
下面是我的userStore方法来验证用户Reactjs 函数中的可观察状态未更新,reactjs,mobx,mobx-react,mobx-react-lite,mobx-react-form,Reactjs,Mobx,Mobx React,Mobx React Lite,Mobx React Form,下面是我的userStore方法来验证用户 @action loadUserByUserId = async () => { try{ const serializedState = sessionStorage.getItem('state'); var userId = '' if(serializedState !== null){ const jsonState = JSON.parse(serializedS
@action loadUserByUserId = async () => {
try{
const serializedState = sessionStorage.getItem('state');
var userId = ''
if(serializedState !== null){
const jsonState = JSON.parse(serializedState);
userId = jsonState.oidc.user.profile.email;
}
const userDetails = await agent.User.loadUserByUserId(userId.toLocaleUpperCase());
runInAction('user found',() => {
this.isUserFound = true;
this.user = userDetails;
});
}
catch(error){
runInAction('user not found',()=>{
this.isUserFound = false;
this.user = null;
});
}
}
验证后,来自OIDC的回调将发送到下面的callback.tsx,如果验证成功,则需要验证用户并导航到应用程序,否则重定向到登录屏幕
Callback.tsx
import React, { useEffect, useState } from "react";
import { connect } from "react-redux";
import { CallbackComponent } from "redux-oidc";
import userManager from "../../features/user/utils/userManager";
import { useHistory } from "react-router-dom";
import { LoadingComponent } from "./LoadingComponent";
import { useContext } from "react";
import { RootStoreContext } from "../stores/rootStore";
import { toast } from "react-toastify";
import { observer } from "mobx-react-lite";
const CallbackPage = (props:any) => {
const rootStore = useContext(RootStoreContext);
const {
loadUserByUserId,
user,
isUserFound
} = rootStore.userStore;
const history = useHistory();
const successCall =() =>{
console.log("Azure Ad Login Success");
console.log(user);
loadUserByUserId();
if(isUserFound){
window.localStorage.setItem('isLoggedIn','true');
history.push('/welcome');
}
else{
window.localStorage.setItem('isLoggedIn','false');
history.push('/');
toast.error("User Not Found, Please add User");
}
}
return (
<CallbackComponent
userManager={userManager}
successCallback={successCall}
errorCallback={error => {
window.localStorage.setItem('isLoggedIn','false');
history.push('/');
console.error(error);
}}
>
<div><LoadingComponent content='Authenticating ...' /></div>
</CallbackComponent>
);
}
export default connect()(observer(CallbackPage));
import React,{useffect,useState}来自“React”;
从“react redux”导入{connect};
从“redux oidc”导入{CallbackComponent};
从“../../features/user/utils/userManager”导入userManager;
从“react router dom”导入{useHistory};
从“/LoadingComponent”导入{LoadingComponent};
从“react”导入{useContext};
从“./stores/rootStore”导入{RootStoreContext};
从“react toastify”导入{toast};
从“mobx-react-lite”导入{observer};
const CallbackPage=(道具:任意)=>{
const rootStore=useContext(RootStoreContext);
常数{
loadUserByUserId,
用户,
iUserFound
}=rootStore.userStore;
const history=useHistory();
const successCall=()=>{
console.log(“Azure Ad登录成功”);
console.log(用户);
loadUserByUserId();
if(isUserFound){
setItem('isLoggedIn','true');
history.push('/welcome');
}
否则{
setItem('isLoggedIn','false');
历史推送(“/”);
toast.error(“未找到用户,请添加用户”);
}
}
返回(
{
setItem('isLoggedIn','false');
历史推送(“/”);
控制台错误(error);
}}
>
);
}
导出默认连接()(观察者(CallbackPage));
问题是即使在函数调用执行之后,isUserFound值仍然为false,并且它总是重定向到登录屏幕。我们如何更新或维护函数中的状态