3 ms·
For some reason, I see people in this thread whom I believe don't really understand why hooks were invented. Put simply, just like closures hold state in a func
by satvikpendem 3y ago
For some reason, I see people in this thread whom I believe don't really understand why hooks were invented. Put simply, just like closures hold state in a function, hooks compartmentalize the component's lifecycle, they are basically `function(state, lifecycle)`. I linked to a Flutter thread elsewhere (on the topic of adding hook-like constructs to Flutter as well) that I've seen as one of the best examples of why class components simply cannot replicate the effects of hooks, due to the inherent limitations of classes [0].
The author uses an example of needing to initialize and dispose of a variable, and you must do this manually each time, for each variable. With a hook, you can abstract this behavior and say, given this lifecycle, I need this variable to be automatically initialized and disposed at the correct time. I write it once, and don't have to think about doing it manually anymore. Hooks are an abstraction over lifecycles, just as functions and loops are an abstraction over repeated procedures.
[0] https://github.com/flutter/flutter/issues/51752 https://github.com/flutter/flutter/issues/51752
- cooperadymas 3y agoI am certain your comment was well intentioned, but it is quite striking how frequently the you are arguing against it, so clearly you must be an idiot defense pops up any time someone criticizes React[0]. [0] https://news.ycombinator.com/item?id=35278169 https://news.ycombinator.com/item?id=35278169
- satvikpendem 3y agoI'm not saying they're an idiot, but it takes time to learn tools. Frontend devs and React devs in particular are usually more novice than other types of coders (in my experience), likely due to so many courses pushing out MERN devs over the last 10 years or so, so it is very likely that most just went with the flow and never actually sat down to read the docs and understand their tools.
- notpachet 3y agoIsn't this an implicit argument in favor of simpler tools with less thorny undersides than React Hooks?
- satvikpendem 3y agoSure, but every technology has its own problems. It just depends which tradeoffs you want to make.