How to use Async Await inside react useEffect() hook

Posted on February 19, 2022

The following method is a safe way to use await and async inside react useEffect() hook

const getUsers = async () => {
    const users = await fetchUsers();
    setUsers(users);
};

useEffect(() => {
  getUsers(); // run it, run it

  return () => {
    // this now gets called when the component unmounts
  };
}, []);
to join this conversation on Samprix.Already have an account? Sign in to comment
SPONSORS