Async functions with React's useEffect hook

It may not be pretty, but the easiest way to call an async function in useEffect is to use an Immediately Invoked Function Expression (IIFE).

useEffect(() => {
  (async () => {
    try {
      await doSomething();
    } catch (err) {
}, []);

That's it! Don't overthink it.

Find me on Twitter for other straightforward solutions.

Free email course

Get the free email course and newsletter for developers ready to put stressful tech interviews behind them and start flourishing in their careers.

We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to and affiliated sites.