DEV Community

Igor Quirino
Igor Quirino

Posted on • Originally published at wareboss.com

6 1

React Hook – Async function in useEffect

From: https://wareboss.com/react-hook-async-function-in-useeffect/

If you already know the error message:

"React Hook Warnings for async function in useEffect: useEffect function must return a cleanup function or nothing"

Here is a little explanation of why this occurs and how to solve them.

This issues occurs when you directly call a Promise from useEffect Hook.

export default function Example() { 
    const [data, setData] = useState(false)

    useEffect(async () => {
      let response = await fetch('api/data') //Direct call
      response = await res.json()
      setData(response)
    }, []);

  return <div>{data}</div>;
}
Enter fullscreen mode Exit fullscreen mode

The useEffect hook expcect to receive a function to cancel or release resources.

To solve this issue you need to call a Syncronous method. Event if this new one are Async.

export default function Example() { 
    const [data, setData] = useState(false)

    useEffect(() => {
      const runAsync = async () => {
        let response = await fetch('api/data')
        response = await res.json()
        setData(response)
      };
      runAsync();
    }, []);

  return <div>{data}</div>;
}
Enter fullscreen mode Exit fullscreen mode

You can cancel this fetch when the component get destroyed, but this is another article (React Hook – Clean Up useEffect).

Bye!

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (7)

Collapse
 
dance2die profile image
Sung M. Kim
Comment hidden by post author
Collapse
 
iquirino profile image
Igor Quirino

Hi Sung,

Thank you by your recommendations.

I'll read more about how to do this and i'll work to publish full articles okay? (I'm new on this platform, i see another article doing exactly the same..)

Thank you!

Bye bye!

Collapse
 
dance2die profile image
Sung M. Kim

Sounds good, Igor & welcome to DEV 🙂

i see another article doing exactly the same..

There are so many posts that slip thru 😅 but we try to suggest to post a full content as much as we can.

Thread Thread
 
iquirino profile image
Igor Quirino

Done. Is it ok now? Thank you! ;)

Thread Thread
 
dance2die profile image
Sung M. Kim

Looks much better~ 😃 Thank you, Igor.

And as a tip, you can also highlight code using triple backticks with language.

demo

Thread Thread
 
iquirino profile image
Igor Quirino

Wow! This looks better!! Thank you Sung! I appreciate your help!
dev.to has a tutorial with best practices?
Thank you!

Thread Thread
 
dance2die profile image
Sung M. Kim

You're welcome there, Igor :)

Unfortunately, there isn't a best practice but there isn't a "best practice" documentation 😅. (The syntax highlight is a nice-to-have).

It's a good idea so would you be able to create an issue requesting a best practice documentation?

github.com/thepracticaldev/dev.to/...

Some comments have been hidden by the post's author - find out more

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay