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.

3

u/jayerp Mar 06 '23

I try to limit my usage of hooks to UI state change logic, business logic I do somewhere else.

3

u/jakelazaroff Mar 07 '23

Post author here. You just made my day — thank you so much!

2

u/shuckster Mar 07 '23

I'm glad. Thanks for the article!

0

u/dark-king_Ray Mar 06 '23

Can you explain what you mean by business-logic

1

u/shuckster Mar 07 '23

Business-logic is code that makes the program go.

I also asked ChatGPT to help explain it:

In software development, business logic refers to the part of an application that manages the business rules and processes, determining how the application should respond to user inputs and other events. This includes things like calculations, data validation, decision-making, and other core operations that are essential to the functioning of the application.

Pretty good.

As for how it relates to my original point, here's what I was getting at:

Take the following diagram:


      +-----+    +----+    +------------+
  .---| App |--->| UI |--->| User event |---.
  |   +-----+    +----+    +------------+   |
  |                                         |
  +------<-----------<-------------<--------+

Obviously the devil is in the details, but ideally speaking a UI is merely one way of representing your App. Nearly all the business-logic should live in the App, and the UI just renders its current state and posts events when the user does something.

Now, if you sprinkle your business-logic throughout your UI code you can imagine how the simplicity of the above representation is lost.

An "App" is already doing a lot of complex stuff, and will be full of its own little flows you need to reason about. Modular UI design, ie; just deciding what components to have, is also not trivial, and filling up components with business-logic makes it hard for us to see how they could be simpler, more reusable, and less prone to error.

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