3 ms·
I just don't understand this take, every time I hear it I wonder if people just haven't spent the time to adjust their mental model. Hooks are IMO the best thi
by ervine 5mo ago
I just don't understand this take, every time I hear it I wonder if people just haven't spent the time to adjust their mental model.
Hooks are IMO the best thing that happened to react.
- pjmlp 5mo agoI have spent more time than I wished for on React debugging tools, and useXXXX spaghetti calls.
- ervine 5mo agoYou can write spaghetti with class components, too. Doesn't sound like a hooks issue to me.
- pjmlp 5mo agoI certainly can, but somehow it cooler to write lambdas upon lambdas, and other Haskellisms when using hooks.
- ervine 5mo agoI think we're just gonna agree to disagree. Cheers.
- halflife 5mo agoOf course, the only question is in what way the programming language pushes you. You can TS code where every variable is assigned “any” type, that would void all type safety. Is it possible? Yes. But someone needs to bend the languages will to do that. Classes have inherent state, and methods which encapsulate logic. That pushed you to create separate logic bundles. Functions are callable scripts. There are no rules. I’ve seen too many components that invoke tens of hooks, and hundreds of lines of state management. Most classes I’ve seen never reached that. Were there classes that were too big? Of course, but at least there was logic separation with methods.
- bobthepanda 5mo agoThe great sin of the migration to hooks was that the docs were so poorly written that everyone got into bad habits. You can have self documenting state like class components in the form of reducers which are just state machines. But it is much later in the documentation.
- halflife 5mo agoI’ve done plenty of hooks. Also class components. Also angular. You are writing code for an object that is inherently stateful, in a stateless design pattern. Instead of embracing state, you crate escape hatches and plugins to tap into your state, and then more escape hatches inside those escape hatches to tap out. It’s react rendering model leaking into your code. Let’s imagine react changes the rendering paradigm, and components are rendered once with only state updates. Almost all of your hooks become useless. The fact that you need to think about react internal every time you create a component is such a bad API choice that I’m amazed it still exists, and being expanded.
- ervine 5mo agoSo your argument is that instead of explicit class component methods, hooks are implicit based on understanding the react rendering model? I guess so - but react could also change (and I think did at some point) how their class methods work, how often they're triggered, and when. I don't understand the stateless comment - hooks are as stateful as you make them, using useState or useContext or any of the other ways of maintaining data between renders.
- halflife 5mo agoClasses are inherently stateful. A class instance is a long lived object. Functions are not, a functions internal state is thrown the moment the function returns. What react ask you to do is to attach to external state inside the function, in other words, an un pure function, forego idempotency. In class based components, you didn’t care how react works under the hood, except for the render method which is called by react. So the surface of code controlled by react was only what you included inside that method. In function components, the entire function is owned by the renderer, so you need to deeply understand how it works.
- ervine 5mo agoI guess I think it's table stakes to need to deeply understand how the framework you're using works. Lifecycle methods vs. hooks, you still gotta know what's doing what and why.