[英]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
touseEffect
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个选项。
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]);
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.