I also dont like the idea of 'useState' overwriting the whole state object.
I also dont like the idea of 'useState' overwriting the whole state object.
How do you arrive at nested functions? Hooks have generally flattened my indentation, and they are refreshingly easy to abstract over, making way for even cleaner code.
For example, I have the following custom hook in multiple components:
useNavigationWarning(() => !!commentBody)
which adds an event listener to the window that warns the user about unsaved work, but only if `commentBody` has any content. Because it's a function, I can reuse the hook and swap out the callback body's conditional for each situation.With class components I would either have to use a higher-order component (bad and clunky), or copy/paste code across two separate lifecycle methods across each of my components that need this behavior.
The thing I enjoy with hooks is that you no longer have to juggle state/async in different methods. I'd usually forget cleaning something up, leading to longer dev-debug cycles. With useEffect I usually remember to clean up.
Also, so far the code we've written with hooks looks (subjectively) nicer than their class counterparts.
One difference between us might be that you have more experience than me using "classic" React. I'm relatively fresh to the React game, so I don't have many old habits or intuitions formed.