3 ms·
I have come to prefer hooks, but I have never felt they're necessary. Class based React allowed for massive simplification of some setup/tear-down actions.
by SkyPuncher 4y ago
I have come to prefer hooks, but I have never felt they're necessary. Class based React allowed for massive simplification of some setup/tear-down actions.
- thebeastie 4y agoFor someone to arrived late to the party, what’s better / simpler when using classes?
- SkyPuncher 4y agoEverything in hooks is explicit. Which is a blessing and a curse. Classes were really easy to compose and automatically easily bring everything you needed into the class. This did lead to some situations where you could over-engineer and get yourself into a bind. You can technically do the same/similar thing with hooks, it just requires a bit more code. Lack of setup/tear down with hooks also means you need to rely on nonces more often. In general, hooks have a bunch of small, reactive-programming tricks that you learn with experience.
- ajdegol 4y agoDepends on your use case; some things are easier as classes others as hooks.
- notatoad 4y agoi'm a react noob too, but my impression is mostly that hooks are nicer for smaller components with simpler states, and classes reduce some boilerplate if you have a lot of different state variables within one component.
- yamtaddle 4y agoHook-using functions basically are classes, just with utterly bizarre method and property declaration syntax and some strange and unhelpful behaviors. I mean, they ultimately associate lists of properties and functions (methods, if you like) with a component object instance (yes, object) down in the React internals. Sure, access to them is FIFO rather than a lookup table, but that still looks a whole lot like very-weird classes/objects being reimplemented in a language that already has decent-enough ones. Now, the trouble was certain optimizations and features were probably going to be hard to impossible to achieve while supporting both functional- and class-based components more-or-less equally, and the easy path forward was to let class components become more powerful and functional components slip to second-class (haha), but instead they put object/class-like features in their functions so they could have functions (but very much stateful/side-effecty, so WTF is the point?) lead the way, for whatever reason.
- barbecue_sauce 4y agoI stopped using React around the time hooks were introduced, and never figured out what problem they were supposed to solve.
- cercatrova 4y agoI have something that has a lifecycle and I don't want to initialize and destroy the state every time. I encapsulate that into a function, call it a hook, and go about my day knowing that I don't have to care about redoing the lifecycle every single time. It's basically a class that has pre-written lifecycle functions.