DEV Community

Discussion on: Why Effects shouldn't lie about their dependencies

Collapse
 
dance2die profile image
Sung M. Kim

Thank you for detailed explanations on how useEffect deps work and how not to lie about it 😀

I have a question regarding 2. Demystifying the cause of the bug,

React always cleans up the previous effects before applying the next effects, it will not bother running the cleanup in this case

I just tried the code snippet and the clean up occurs.

demo

Demo code above

What would you mean by the clean up never running?

Collapse
 
aman_singh profile image
Amandeep Singh • Edited

Hi,
In your code snippet, you are setting alternate key (true and false) on every click handler to the div element. key plays an important role in React reconciliation process. Setting a different key on your div element is like asking React to recreate the whole tree, which follows the process of unmounting the component and remounting it. And doing so, it runs the cleanup effect as it will do in the normal umount cycle.
Hope it helps 🙂

Collapse
 
dance2die profile image
Sung M. Kim

Thanks Amandeep for the explanation, links (and the nice word highlights) 😀

Thread Thread
 
aman_singh profile image
Amandeep Singh

Thanks Sung. Happy that it was helpful. 😀