Problem: The React docs encourage declaring anonymous functions in useEffect


Problem: The React docs encourage declaring anonymous functions in useEffect. This means the developer's intent isn't documented. 👎

Solution: Call a well-named function inside useEffect.

#react

I find this habit clarifies my thinking. It forces me to declare my intent via a well-named function first.

And later, when someone reads my code, they know exactly what I was trying to do.

useEffect only specifies *when* code runs. So, it's my job to describe *what* I'm doing.
The named function might feel unnecessary for this simple example. In real life, things tend to be more complicated.

Calling named functions inside useEffect becomes especially useful when:

- The useEffect is complex
- Or, there are multiple useEffects
Oops, forgot to add alt text to the image in the first tweet, so adding here. Full code is in the alt tag.

View original on X