DEV Community

Discussion on: Common UseEffects mistakes that even seniors are making

Collapse
 
calag4n profile image
calag4n • Edited

I think you've made a mistake (or I didn't get your point) :

useEffect(callbackFunction) in here, callbackFunction is not executed only once after the first render but after each render ;
and
useEffect(callbackFunction, []) is not the same as useEffect(callbackFunction) , when you provide an empty array it's precisely in this case that your function is executed only once.

Some other writing mistakes with mixed up terms : myVar , var1 , var2 , var , myObject .

Nice post otherwise 👍️

Collapse
 
sleeplessbyte profile image
Derk-Jan Karrenbeld

The post is full with bad advice. It's a good attempt, but a bad resource.

For example:

In example 3 you must always use primitives as dependencies never reference types such as *Functions, Objects or Arrays. Use only string, boolean, number.

You can use whatever you want as a dependency in the second argument of a useEffect hook, but if you want it to work as intended, the value must be stable: that is, not be regenerated each render. You can use functions, objects, and arrays.

Collapse
 
hernanif1 profile image
Hernani Fernandes • Edited

Hi Derk Jan,
Thank you for your feedback.
I updated this post so I removed that wrong typos and I'm trying to make my point clearer.
About the primitives as dependencies I'm trying to explain that most of the devs are simply filling in this parameter and are not aware about why.
Let me know if you have any resource that explains better about this specific part.

This is my first post so I was trying to make it fine instead of make it perfect because I procrastinated it for a long time.
Thank you for helping me to make it better.

Collapse
 
hernanif1 profile image
Hernani Fernandes

Hi calag4n, thank you for your feedback.
I didn't know about this difference between with /without array so thank you very much for teaching me that.
I updated the post.

Collapse
 
calag4n profile image
calag4n

Glad to help 😊