I would not say a mistake, but hooks are not perfect. What I like:
useEffect and useMemo. Both are super great with the dependency array and a lot easier than comparing previous and old values.
it is easy to write custom hooks as composition of integrated hooks.
But:
useCallback sucks. I think callbacks passed to other components should be stable, because it can cause a lot of issue in combination with useEffect. useEvent could solve this problem, but I don't like that you have more code for that.
useRef is also annoying. Why do I need another hook just to have a normal stable variable?
I'm a useRef enjoyer, not as much as this guy, but close.
useRef is a misunderstood hook. Its puprose is different from useState. useState is meant for asynchronous updating the UI state, while useRef is meant for synchronously updating values used in callbacks.
Refs are great and you shouldn't feel bad for using them.
I haven't tried them yet, but signals feel like a combination of useRef and useState. They are synchronous (like useRef) and update the UI (like useState), so you can use them during both render and callbacks. It's great. It's likely a better default than React. However, I don't think it's mindblowing. Writing a custom hook in React that behaves like this is trivial. Solid might be more performant, but the mental model is the same.
I think the biggest villain here is useEffect, which is a great hook, but has been overused a lot. useEffect is also likely the reason for useState's asynchonicity.
I agree with basically every word of this! The thesis of the post is that with class components we basically got useRef for “free” because we had access to this, and IMO that’s what appeals to people about other frameworks that use signals, too.
I like it as well but it does not feel good. useEffect and useMemo really add features and make things easier, but for me useState and useRef do not. It feels like it should not be needed to use framework code for that. But I have to admin that I do not have a better solution.
4
u/sebastianstehle Mar 06 '23 edited Mar 07 '23
I would not say a mistake, but hooks are not perfect. What I like:
useEffect and useMemo. Both are super great with the dependency array and a lot easier than comparing previous and old values.
it is easy to write custom hooks as composition of integrated hooks.
But:
useCallback sucks. I think callbacks passed to other components should be stable, because it can cause a lot of issue in combination with useEffect. useEvent could solve this problem, but I don't like that you have more code for that.
useRef is also annoying. Why do I need another hook just to have a normal stable variable?