Learning React: 5 Important Principles About Hooks You Have to Know
code.likeagirl.io
code.likeagirl.io
It's clear that the existing rules are confusing: hooks look like functions but follow special rules, you need to manually manage hook dependencies with crutches like useCallback / useMemo, etc.
Just adding a few keywords like "hook" and "use" could make it clearer and allow the build tool to automatically generate some of the boilerplate like useCallback.
The example in the article might look something like this (leaving out the TypeScript annotations):
hook boolean(initialValue) {
use [value, setValue] from hook state(initialValue);
return [
value, {
setFalse: () => setValue(false),
setTrue: () => setValue(true),
toggle: () => setValue(!value)
}
];
}
Behind the scenes this would generate a useBoolean() hook that can be called directly from plain JS if needed, just like JSX generates createElement() calls.IMO that was a major lesson from C++ — it wasn’t all that clever to allow code that looks like C and would pass a C compiler but does something completely else.
Your position is more like saying:
“Why does C++ need special syntax for initializers? Surely you can just use alloca() to grab a variable-sized blob of bytes from the stack and cast it to whatever types you’re initializing.”
The `defineModel` macro from release 3.4[0] is a prime example where a really simple macro completely changes my workflow and makes refactoring complex components a breeze.
[0] https://chrlschn.dev/blog/2024/01/a-conceptual-model-of-stat...
I don't use React (I main Vue), so I can't speak authoritatively. But as an outside observer it feels like React gets progressively more and more "mind-bendy" with every passing year, and when I look at some high-complexity React apps my mind nearly explodes with the amount of crazy complexity that needs be managed to keep the things behaving the way they're meant to.
I personally believe a lot of React developers are experiencing the framework equivalent of Stockholm Syndrome. I would encourage any React devs reading this to go look at the Vue composition API and then try to explain why all of these increasingly convoluted abstractions are necessary.
useState -> ref
useEffect -> watchers
useRef -> template-refs
The only difference I see, is that Vue rerenders nothing by default whereas React rerenders everything by default. And I admittedly agree with Vue on this one, but it's not enough to make me give up the superior ecosystem and JSX.
The boilerplate for creating computed's is comparable to setting up useMemo calls. And oh look: the justification for computed is exactly the same as useMemo/useCallback: https://vuejs.org/guide/essentials/computed.html#computed-ca...
Also, the the counter and the input field example is useless and contrived. And I realise they tried to keep the code simple to illustrate usage, but the author managed to make the example homeopathic. They should come up with a better example (ie the one in the React documentation is pretty good at illustrating why refs are useful).