首页 > 解决方案 > 注销时出现 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 上的令牌。

标签: reactjsreact-hooks

解决方案


推荐阅读