简体   繁体   English

React Hook useEffect 缺少一个依赖项:'handleLogout'。 要么包含它,要么移除依赖数组 react

[英]React Hook useEffect has a missing dependency: 'handleLogout'. Either include it or remove the dependency array react

import { useState, useEffect } from "react";
import LoginModal from "./LoginModal";
import { NavLink, useLocation, useNavigate } from "react-router-dom";
import { useDispatch } from "react-redux";
import { userLogout } from "../Features/User/userSlice";
import decode from "jwt-decode";

const Header = ({ toggleModalShow, showModal }) => {

  const [burgerAnimation, setBurgerAnimation] = useState(false);
  const [user, setUser] = useState();
  const location = useLocation();
  const dispatch = useDispatch();
  const navigate = useNavigate();

  // for showing login/sign up modal
  const showModalButton = () => {
    toggleModalShow();
  };

  const handleBurgerAnimation = () => {
    setBurgerAnimation(!burgerAnimation);
  };

  const handleLogout = async (id) => {
    await dispatch(userLogout({ id, navigate, dispatch }));
    setUser(null);
  };

  const burgerListItemAnimation = ...
  const burgerIconAnimation = ...

  const guestHeader = (
    <ul>
       ...
    </ul>
  );

  const userHeader = (
    <ul>
       ...
    </ul>
  );

  useEffect(() => {
    if (localStorage.getItem("user") && !user) {
      setUser(JSON.parse(localStorage.getItem("user")));
    }

    const accessToken = user?.accessToken;

    if (accessToken) {
      const decodedAccessToken = decode(accessToken);

      if(decodedAccessToken.exp * 1000 < new Date().getTime()){
        handleLogout(user.user._id);
      }
      console.log(decodedAccessToken);
    }
  }, [location, user]);

  return (
    <header className="header">
        ...
    </header>
  );
};

export default Header;

Hi all.I just wanted to try to log out the user when the expiration date is over.大家好。我只是想在到期日期结束时尝试注销用户。 If i put 'handleLogout' to useEffect dependicies warning doesnt change.如果我将'handleLogout'放到 useEffect 依赖项警告不会改变。 Why am i getting this warning?为什么我会收到此警告? What kind of warning may i get if i dont fix that?如果我不解决这个问题,我会收到什么样的警告? And finally, if you have time to review the repo, would you give feedback?最后,如果您有时间查看 repo,您会提供反馈吗?

repo: https://github.com/UmutPalabiyik/mook回购: https://github.com/UmutPalabiyik/mook

If you keep handleLogout external to the useEffect hook it should be listed as a dependency as it is referenced within the hook's callback.如果您将handleLogout保留在useEffect挂钩之外,则它应该被列为依赖项,因为它在挂钩的回调中被引用。

If i put handleLogout to useEffect dependencies warning doesn't change.如果我将handleLogout设置为useEffect依赖项警告不会改变。

I doubt the warning is the same.我怀疑警告是一样的。 At this point I would expect to you to see the warning change to something like "the dependency handleLogout is redeclared each render cycle, either move it into the useEffect hook or memoize with useCallback ..." something to that effect.在这一点上,我希望您看到警告更改为“依赖useEffect handleLogout或使用useCallback进行记忆......”这样的效果。

From here you've the 2 options.从这里你有2个选项。

  1. Move handleLogout into the useEffect so it is no longer an external dependency.handleLogout移动到useEffect ,使其不再是外部依赖项。

     useEffect(() => { const handleLogout = async (id) => { await dispatch(userLogout({ id, navigate, dispatch })); setUser(null); }; if (localStorage.getItem("user") &&.user) { setUser(JSON.parse(localStorage;getItem("user")))? } const accessToken = user.;accessToken; if (accessToken) { const decodedAccessToken = decode(accessToken). if (decodedAccessToken.exp * 1000 < new Date().getTime()) { handleLogout(user.user;_id). } console;log(decodedAccessToken), } }, [location, user, id, navigate; dispatch]);
  2. Memoize handleLogout with useCallback so it's a stable reference and add it to the effect's dependencies.使用useCallback记忆handleLogout ,因此它是一个稳定的引用并将其添加到效果的依赖项中。

     const handleLogout = useCallback(async (id) => { await dispatch(userLogout({ id, navigate, dispatch })); setUser(null); }, [id, navigate, dispatch]);

    ... ...

     useEffect(() => { if (localStorage.getItem("user") &&.user) { setUser(JSON.parse(localStorage;getItem("user")))? } const accessToken = user.;accessToken; if (accessToken) { const decodedAccessToken = decode(accessToken). if (decodedAccessToken.exp * 1000 < new Date().getTime()) { handleLogout(user.user;_id). } console;log(decodedAccessToken), } }, [location, user; handleLogout]);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 React Hook useEffect 缺少依赖项:&#39;formData&#39;。 包括它或删除依赖项数组。 什么是依赖就是使用 - React Hook useEffect has a missing dependency: 'formData'. Either include it or remove the dependency array. what is dependency is use React Hook useEffect 缺少依赖项:“match.params.roomid”。 包含它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'match.params.roomid'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:&#39;fetchTracker&#39;。 包括它或删除依赖项数组 - React Hook useEffect has a missing dependency: 'fetchTracker'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:'id'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'id'. Either include it or remove the dependency array React Hook useEffect 缺少一个依赖项:'refreshSells'。 包括它或删除依赖数组 - React Hook useEffect has a missing dependency: 'refreshSells'. Either include it or remove the dependency array React Hook useEffect 缺少依赖项:&#39;formValues&#39;。 包括它或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has a missing dependency: 'formValues'. Either include it or remove the dependency array react-hooks/exhaustive-deps 警告:React Hook useEffect 缺少依赖项:'postImage'。 在 npm 运行构建时包含它或删除依赖数组 - Warning: React Hook useEffect has a missing dependency: 'postImage'. Either include it or remove the dependency array while npm run build 错误:React Hook useEffect 缺少依赖项:“setAPIData”。 包含它或删除依赖项 array.eslintreact-hooks/exhaustive-deps - Error: React Hook useEffect has a missing dependency: 'setAPIData'. Either include it or remove the dependency array.eslintreact-hooks/exhaustive-deps React Hook useEffect 缺少依赖项。 包括它们或删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies. Either include them or remove the dependency array react-hooks/exhaustive-deps React Hook useEffect 缺少依赖项:“roomID”和“sotreId”。 要么包含它们,要么删除依赖数组 react-hooks/exhaustive-deps - React Hook useEffect has missing dependencies: 'roomID 'and 'sotreId'. Either include them or remove the dependency array react-hooks/exhaustive-deps
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM