Skip to content Skip to sidebar Skip to footer

Component-updating Custom Hook In Useeffect

I have the following code: function checklogin(callback) { if (!state.user.authenticated) pushhistory('/accounts/login', function(){teamhome2_message();}); else callb

Solution 1:

I think the solution is to move the useEffect call inside the useLogin hook, like so:

function useLogin(state, url, loginMessage) {
  const history = useHistory();
  const loggedIn = state.user.is_authenticated;
  useEffect(() => {
    if (!loggedIn) {
      history.push(url);
      loginMessage();
    }
  }, [loggedIn]);
  return loggedIn;
}

Post a Comment for "Component-updating Custom Hook In Useeffect"