DEV Community

Discussion on: Custom React Hooks: useBoolean

Collapse
fidaay profile image
fidaay

Why not just

setShowSpoil(!showSpoil);
Enter fullscreen mode Exit fullscreen mode

?

Collapse
kraskaska profile image
Kraskaska

well yes but actually no

Collapse
iamludal profile image
Ludal 🚀 Author • Edited on

It is a good practice to use the "function" form since the new value depends on the previous statue value. In most cases, both solutions will work correctly, but it is possible to have some kind of desynchronization (and so unexpected behaviors) when using your version.

A few keys here (though it is not exactly the same question as you) : stackoverflow.com/questions/610542...

Collapse
fidaay profile image
fidaay

That "desynchronization" occurs because of states mutations are asynchronous, and it shouldn't be treated as a problem nor unexpected behavior.