How to use async functions in useEffect

The useEffect is the place where we mostly write code to get data. Data fetching is an asynchronous function so we can not use async/await in straightforward ways. In this article, we will show you how to use async/await functions in useEffect.

The Wrong Way

Don’t do the following approach to call the async function in the useEffect.

The Correct Way

The correct way is that you have to define the async function within useEffect and call it.

This is another way to write the code above as follows.

async function with useCallback

Use the below code if you want to use async functions outside with eslint.

I hope you find this article helpful.
Thank you for reading. Happy Coding..!! 🙂

If you found value in this article,
you can support us by buying me a coffee! ☕

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *