5 ms·
If you have an understanding of closures, hooks are quite intuitive.
by phist_mcgee 2y ago
If you have an understanding of closures, hooks are quite intuitive.
- recursive 2y agoI have a pretty thorough understanding of both. But I can't understand how you'd find them to be similar in any way. Functions forming closures can be called conditionally, in a loop, or even when no react component is even rendering. Most of the complexity of hooks is not addressed at all by closures, and I don't really what part of their behavior is related at all. Maybe just that you can pass a value to one function, and then get it returned from another one. The standard hooks delegate to a dispatcher, which has access to the current fiber. The current fiber has a linked list of memoizedState for the current work node. It's true that a lot of the functions that eventually service the hook calls do contain closures. But that doesn't seem to grant much insight into how to use them or how they work. It's like saying "if you have an understanding of loops, the reconciler is quite intuitive". I mean yes, the reconciler uses loops. But the behavior of reconciliation may still be quite mysterious.
- kaoD 2y agoI'll try to fix the comment you're replying to: I think of React components as not closures but coroutines (and hooks are its yield points). Hooks are implicitly keyed by index which is the most magic-y/surprising part... but I'm sure if you had to manually key them that'd be criticized too (and would be abused to death-by-bugs, so I can see why they went with this design). If you understand both points above, you understand hooks. I still fail to get the (usual) criticism of hooks. They have lots of warts but the API (which is often what gets mentioned) is the most superficial and less annoying criticism. Feels like something that would be mentioned after just skimming the docs. Very shallow. On the contrary: useEffect is a footgun and deserves criticism. useRef being overloaded to mimic instance variables is confusing for newbies (but this is just a naming issue IMO). The difference between normal/layout/insertion effects is complex and subtle. The new stuff that tries to solve some issues with the concurrent mode (like transitions/deferred value/etc.) feels like a huge hack. React 19 will come with its own warts (actions, RSC...) But the API? I don't care at all. Pretty simple, at least for my mental model.
- com2kid 2y agoThe complaint is they prior to hooks react didn't have any of this complexity. It was pretty simple to understand. Class components works mostly as you expected them to. There are a handful of things that were really hard to do with class components that hooks + function components made easier, but lots of other things became more complicated with each new set of features react has added since then. At this point the solution to "class components are complicate" is far more complicated than class components ever were.
- kaoD 2y agoI understand the complaint but my point is that, in my experience (which admittedly might be biased), the complaint usually does not resonate with anyone that did actually use React for at least a moderately complex app. Hook's API is not perfect but it's a good-enough abstraction that allows the user to have even better abstractions and separation of concerns. Actual React users did not care about that because the pragmatism far outweighs the theoretical ugliness... which honestly is not even that ugly if you have a mental model similar to coroutines (of course if all you do is OOP a class will look better to you...) I have recently been fixing some stuff in my old React pre-hooks code and I hated it because class-based components had all sorts of concerns intermixed on their lifecycle methods... no matter how much you tried to abstract them. Abstracting those into reusable hooks was a breeze and made everything much easier to follow and maintain. Hooks are far better from a pragmatic point of view. > There are a handful of things that were really hard to do with class components that hooks + function components made easier, but lots of other things became more complicated Like what? Does not match my experience at all.
- watwut 2y ago> of course if all you do is OOP a class will look better to you... Funny thing is, I do not know what coroutines are and have zero experience with them. I am object oriented class programmer who never cared about functional programming. I figured hooks intuitively and straightforwardly when I had to work with react a year ago. Ok, function with state, but it was easy to read them, imagine what they do and maintain the code. It just seemed as an improvement over the old react to me, despite having no coroutines knowledge.
- imtringued 2y agoHooks have very little to do with closures. The key aspect of hooks is that what you are doing is something like this: function(context) { context.useState() } Where context is a variable that stores all the hook related data, except in reality that variable is a hidden global variable and useState() accesses that variable internally. Yeah, they added hooks as global functions so that they look like a "cute" DSL. It saves you the effort to type c.useState instead of useState I guess. The above code is just for illustrative purposes to get the idea across, according to other commenters the internal implementation has changed from what I remember, but the principle is still the same. Global functions demand global state.