by simplify · 7y ago · 0 comments
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.
No comments yet.