DEV Community

Discussion on: 3 amazing REACT HOOKS to keep your code organized neatly

Collapse
 
efleurine profile image
Emmanuel

Thanks for sharing.
I have some comments

1) you have forgotten to await in your function in the first snippet so it should have been like

const result = await doSomeAction();

Enter fullscreen mode Exit fullscreen mode

2) I think calling directly an async function in useEffect is not recommended. I remember getting an warning the last time I did it.

useEffect(() => {
    asyncStuff()
  }, [])

const asyncStuff = async () => {
    setResult(null)
    setError(null)
    setLoading(true)
    try {
      const result = await doSomeAction();
      setResult(result)
    } catch (e) {
      setError(e)
    } finally {
      setLoading(false)
    }
}

Thanks

Enter fullscreen mode Exit fullscreen mode
Collapse
 
dglsparsons profile image
Douglas Parsons

You're absolutely right on both fronts. I'll update the example. Thanks!