r/javascript • u/magenta_placenta • Mar 06 '23
Were React Hooks a Mistake?
https://jakelazaroff.com/words/were-react-hooks-a-mistake/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
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
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.
4
u/BerryNo1718 Mar 06 '23
Honestly that issue is pretty specific to setTimeout and setInterval given their nature. But you can create a useTimeout hook which doesn't suffer from the same issue.
See: https://usehooks-ts.com/react-hook/use-timeout They solve the issue by saving the callback in a ref. All the hooks in that library are well thought and cover common pitfalls like that. Don't try to do it yourself, you'll likely mess it up.
4
u/roofgram Mar 07 '23
useState, useCallback, useMemo, useRef, etc.. are all kind of hacks because you can't store state in a functional component itself. Plus the fact that closures wrap old state result in failure cases you will eventually hit like in this blog post. I've definitely had to revert to class components a few times because of this and it makes me think if usings were a mistake. Classes were more code, but nothing magic; functional components are smaller but need a lot of this 'using' magic to work.
10
u/ILikeChangingMyMind Mar 06 '23
Have you tried programming with class-based React code?
I have, and I promise hooks were not a mistake: if they were, we'd all still be using classes.
4
u/card-board-board Mar 07 '23
Classes with redux and HOCs were a nightmare, and they didn't create an environment to write clean concise code. That being said useEffect, useMemo and useCallback have always felt like hacks. I would prefer to write a function then call that function, not write a function and have it call itself whenever I call a different function to update a variable. I think I'd have rather just had JS classes be less weird than end up with functions that are more concise but follow mystical magical rules.
-1
Mar 06 '23
Whoa whoa whoa let's stay above the belt here. Classes were a mistake from jump, but let's keep it classy but classless 😂
0
u/ILikeChangingMyMind Mar 06 '23
I honestly didn't think there was anything "unclassy" about my comment.
-1
Mar 06 '23 edited Mar 06 '23
It was a joke, that's why the emoji
Edit: also a play on words not a pun
-13
0
Mar 06 '23
[deleted]
7
u/rk06 Mar 06 '23
React was a definite improvement upon existing paradigm. Angular js already had support for components. But they prioritized directives. React forced the component oriented approach.
Hooks too are a great idea. React's implementation of hooks is faulty. Vue and solid do not suffer from those issues
5
11
u/Exoskele Mar 06 '23 edited Mar 07 '23
Hooks are much easier than class components were for 80% of use cases, but the remaining 20% can be a bit hairy if you aren't so familiar with how the underlying system works. I ran into this exact kind of issue when doing mouse move events recently because I was using addEventListener in the component and getting stale state references.
Ultimately useRef is a good way to solve the problem.