【发布时间】:2020-12-06 10:14:17
【问题描述】:
在过去的两天里,我一直在努力解决页面重新加载 reactJS/NodeJS 应用程序进入登录页面的问题。我使用 jwt 身份验证。在每一页上,我在useEffect 中都有loadUser 函数
有功能:
const loadUser = async () => {
if (localStorage.token) {
setAuthToken(localStorage.token);
}
try {
const res = await axios.get("/api/auth");
dispatch({ type: USER_LOADED, payload: res.data });
} catch (err) {
dispatch({ type: AUTH_ERROR });
}
};
// USER_LOADED
case USER_LOADED:
return {
...state,
isAuthenticated: true,
loading: false,
user: action.payload,
loaded: true,
};
页面重新加载应用程序进入登录页面并设置初始状态。看起来函数没有运行,我不明白为什么?
有什么想法吗?
【问题讨论】:
-
确保您的 axios.get 需要前导反斜杠.. 取决于您的 api
-
网址没问题。因为这个功能可以从登录页面到主页,但是在重新加载时它不起作用..状态没有设置,它只是转到登录页面..useefect中的其他功能。
标签: node.js reactjs use-effect context-api