I have a code like this in a component;
const [loggedOut, setLoggedOut] = useState(false);
function logoutHandler(e) {
e.preventDefault();
setLoggedOut(true);
}
useEffect(() => {
setTimeout(() => {
if (loggedOut) setLoggedOut(false);
}, 1);
});
When I click the logout button I want it to be true but then immediately to the false like a flip-flop. I accomplished it with the code shown above kind of. But I am not sure that this is the right approach.
CodePudding user response:
You dont need to pass timeout when logout change then useEffect work.
const [loggedOut, setLoggedOut] = useState(false);
function logoutHandler(e) {
e.preventDefault();
setLoggedOut(true);
}
useEffect(() => {
if (loggedOut) setLoggedOut(false);
}, [logout]);