r/javascript • • Mar 06 '23

Were React Hooks a Mistake?

https://jakelazaroff.com/words/were-react-hooks-a-mistake/
7 Upvotes

24 comments sorted by

View all comments

11

u/shuckster Mar 06 '23

Those are some hot damned beautiful ass fonts on that blog.

As for Hooks, I think their main drawback is it's far too easy to do a whole bunch of business-logic with them. There's a tendency to overuse them for updating state, rather than merely responding to updates of it.

It's not uncommon to see a trampoline of useEffects updating state all over a mildly complicated component, completely betraying the nature of what a component should be: A pure function that represents state as UI, optionally sending events to interested subscribers when the user does something.

While it's falling out of favour due to boilerplate issues, the core idea of Redux sensibly expresses this idea, and solutions to untangle spaghettified business-logic would do well to follow its example.

If you have more than one useEffect in your component, it's probably a signal (no pun intended) that it's time to take a good look at your business-logic and take steps towards lifting it out of there so you can reason about it without worrying about those infernal "rules of hooks".

As for signals themselves, if they don't encourage us to place business-logic in the right place I would guess they will eventually suffer from their own problems. The management of wild state dependency-graphs woven throughout the UI of complex apps, for example.

But hey, if programming were easy everyone would be doing it.

1

u/eternaloctober Mar 07 '23

doing HTML5 canvas programming is one place where useEffect can get a bit crazy. a simple way to do canvas in react is to draw all the stuff in a useEffect. but then you need a very long list of things in the useEffect that are relevant to your drawing code. I made an alternative system that actually redraws the canvas using mobx now, which is quite a bit nicer as it automatically tracks the relevant things. note that there are alternatives like react-konva that add a delcarative api to canvas but haven't really evaluated it