【发布时间】:2022-01-14 16:12:18
【问题描述】:
我无法使用 Context API 登录我的应用程序。当我在变量 session 的 localStorage 中没有任何令牌运行应用程序时,会出现很多错误,如下所示:
Uncaught TypeError: Cannot read properties of null (reading 'name')
我认为存在这个问题是因为我来自ApplicationContext 的currentAccount 为空。
dashboard/index.tsx
const { currentAccount } = useContext(ApplicationContext);
return (
<span>{currentAccount.name}</span>
);
在routes.login 登录页面上,我也遇到了这些异常,即使此错误应该只出现在routes.dashboard 上:/ 刷新页面或清除 localStorage 无济于事。我在 ApplicationContextProvider 中也有一个无限循环 checkLogin :(
登录/index.tsx
const { setCurrentAccount } = useContext(ApplicationContext);
const onFinish = async (email: string; password: string) => {
try {
const response = await axios.post("/auth/login", {
email: email,
password: password
});
const token = response["token"];
const account = response["account"];
if (token && account) {
localStorage.setItem("session", token);
setCurrentAccount(account);
history.push(routes.dashboard)
}
} catch (error) {
}
};
App.tsx
return (
<div className="App">
<Switch>
<ApplicationContextProvider>
<Route path={route.login} component={Login} />
<Main>
<Route path={route.dashboard} component={Dashboard} />
</Main>
</ApplicationContextProvider>
</Switch>
</div>
);
ApplicationContextProvider.tsx
export type AccountContext = {
currentAccount?: Account;
setCurrentAccount: (user: Account) => void;
checkLogin: () => void;
};
export const ApplicationContext = React.createContext<AccountContext>(null);
interface ProviderProps {
children: React.ReactNode
}
export const ApplicationContextProvider = ({ children }: ProviderProps) => {
const [currentAccount, setCurrentAccount] = useState<Account>(null);
useEffect(() => {
checkLogin();
}, [currentAccount]);
const checkLogin = async () => {
const token = localStorage.getItem("session");
if (token) {
const token = localStorage.getItem("session");
const decode = jwt(token);
const query = {
id: decode["id"]
}
const response: Account = await api.get("/auth/account", query);
setCurrentAccount(response);
} else {
setCurrentAccount(null);
}
};
const stateValues = {
currentAccount,
setCurrentAccount,
checkLogin
};
return (
<ApplicationContext.Provider value={stateValues}>
{children}
</ApplicationContext.Provider>
);
谁能告诉我我的上下文逻辑有什么问题,以验证用户到应用程序?
感谢您的帮助!
【问题讨论】:
标签: javascript reactjs typescript