DEV Community

Discussion on: How to use React Hooks - 2 most commonly used Hooks explained ✨

Collapse
 
sushmita094 profile image
Sushmita Pandey

useEffect with no dependency array will run at the first render and after every render. It's used when we might want to run some code on every single render, although this scenario isn't used a lot.
No it won't go in a state of infinite loop unless you are changing any state values inside useEffect, in that case:
state change -> useEffect triggered -> state change -> ....goes on in an infinte loop

Collapse
 
djnadackal profile image
djnadackal

And one more doubt, why return a call back method inside the useEffect?

Thread Thread
 
sebalr profile image
Sebastian Larrieu

That's executed before the next useEffect, for example if you set a timeout inside useEffect, you must cancel it as return callback, so in the next execution, before setting the new timeout, the previously one is good to be cleared

Collapse
 
sebalr profile image
Sebastian Larrieu • Edited

Empty array means only execute after mount. The value in the arrays is what react check for execution. Empty array means nothing to check

Thread Thread
 
djnadackal profile image
djnadackal

Hi Larrieu, I was referring to the scenario when we dont give a dependency array. 🙂