DEV Community

Discussion on: React hooks & the closure hell 2

Collapse
 
anpos231 profile image
anpos231 • Edited

I think I am missing what you are trying to accomplish here? For me everything works as expected.

If you want to know why is console.log('here') being called? It's because callbacks are regenerated on each render, this way you can always access fresh values from your closure. But values inside the handlers object are always the same, so you are passing the same value to your components.

To visualise:
[handler] calls [callback]

[callback] changes on each render
[handler] always stays the same

[handler] is what you are passing to your descendant components.
[callback] is the function that does stuff.

Collapse
 
stereobooster profile image
stereobooster • Edited

If I will click second button, I will see here in the console e.g. function in your hook gets recreated even though state connected to it isn't changed. But I see from the comment, that this is exactly what you want ¯\_(ツ)_/¯