reactjs - 注销时出现 setUseState() 错误不是函数
问题描述
我正在使用 JWT 进行身份验证,并且由于某种原因,如果我在 url 为“/”时从主页注销,它就可以注销。但是,当我从路径为“/:id”的个人资料中执行此操作时,它会显示此错误:
TypeError: setRefreshCheckLogin is not a function
14 | const user = userAuth()
15 | const logout = () => {
16 | logoutApi();
> 17 | setRefreshCheckLogin(true);
| ^ 18 | }
19 | return (
20 | <div className="left-menu">
一旦我刷新它就会注销用户。所以我想弄清楚是什么导致了当 url 为“/”时只注销这个问题。
这是注销功能:
export default function LeftMenu(props) {
const { setRefreshCheckLogin } = props
const user = userAuth()
const logout = () => {
logoutApi();
setRefreshCheckLogin(true);
}
我将 setRefreshCheckLogin 作为 App.js 的道具传递,它在 useEffect 中使用,
const [refreshCheckLogin, setRefreshCheckLogin] = useState(false)
useEffect(() => {
setUser(isUserLoggedInApi());
setRefreshCheckLogin(false);
setLoadUser(true);
}, [refreshCheckLogin])
这就是 logoutApi 的工作方式:
export function logoutApi() {
window.history.replaceState(null, "New Page Title", "/")
localStorage.removeItem(TOKEN)
}
我尝试了 window.history.... 线认为它会清理 url 并使其工作,但它没有。
考虑到如果我从“/”注销并且从技术上讲它可以在其他路径上工作但它显示该错误,我真的没有想法,然后我必须刷新但它成功地摆脱了 localStorage 上的令牌。
解决方案
推荐阅读
- html - 检查第二个 li 元素是否有当前类
- express - GitHub API 没有响应输出
- android - Kotlin - 如何从数据类列表中更新特定值
- firebase - 一个函数打印该值,但返回时它没有打印在另一个函数上
- three.js - Threejs collada 3D模型在某些角度无法正确渲染
- angular - 为什么屏幕截图在离子中没有保存功能?
- jolt - 在 JOLT 中从一种列表格式转换为键值格式
- python - 使用最新日期从数据框中提取最新数据
- google-cloud-platform - DataFusion 中的供应问题
- python - 如何在 django 中重新加载图像的 html src