score:1

Accepted answer

your function, on the outside, should be a dependency of your useeffect. to avoid that, just move it inside the hook. other than that, looks good to me. reading a cookie is a side-effect, so you're doing it the right way.

useeffect(() => {
  const readcookie = () => {
    let user = false;
    if(user = cookies.get("islogged")){
        history.push('/homepage');
        setauthenticated(true);
    }else{
        history.push('/');
    }
  }

  readcookie()
},[]);

if you wanted to leave readcookies outside of the hook, you'll have to wrap it in a usecallback hook and use that in your dependency, like so:

const readcookiescallback = usecallback(() => {/* your read cookies code */}, [])

useeffect(() => {
  readcookiescallback()
}, [readcookiescallback])

for what you're doing, that's unnecessary. my first example is the way to go.


Related Query

More Query from same tag