| Hooks are IMO one of the greatest innovations in UI programming. I'll draw an analogy. --- Why did async/await replace callbacks? They are really the same thing, aren't they? Aren't callbacks good enough? Async/await allowed programmers to write asynchronous code as if it were synchronous. --- Why did hooks replace classes/imperative? They are really the same thing, aren't they? Aren't classes/imperative good enough? Hooks allowed programmers to write stateful code as if it were stateless. function MyComponent() {
const [value, setValue] = useState(false);
const buttonClass = value ? "on" : "off";
return (
<button
className={buttonClass}
onClick={() => setValue(!value)}
>
{String(value)}
</button>
);
}
This is functional code, without scary, hard-to-reason side-effects. Except for one limited part, which operates in a stateful way.I argue that this is simpler than the equivalent imperative-DOM modification code, and the gap between these only increases with real programs. This isn't just a weird "web bro" thing; the hooks paradigm is useful (in theory and practice) to every UI platform. I even wrote an implementation (closed source) for hooks in Angular. It was lovely. |
It is some pseudo DSL with hidden hard to reason mutable state.