3 ms·
> They need to rewrite it to use hooks at some point What is the reason that not using hooks is so frowned upon? When I took my first steps with React, I didn'
by moring 6y ago
> They need to rewrite it to use hooks at some point
What is the reason that not using hooks is so frowned upon? When I took my first steps with React, I didn't know hooks, so I write component classes and it immediately "clicked" with me because it behaves like most other GUI frameworks do, from Win32-based (1) to X-based to Android to iOS to macOS to Java, and tons of higher-level frameworks in between. It also feels obvious to write a component class because you are describing the behavior of a stateful thing, that is, exactly what classes are made for.
With hooks, OTOH, we have a stateful "function", but all state is implicit, which describes behavior other than its main behavior (rendering) but that is implicit too... I can see why developers outside the React world don't "get" hooks, because I can't even remember seeing this idiom outside the React world. (BTW, I've seen the claim that hooks are easier to reason about, for which I found the opposite to be true, and the claim that this makes the main function stateless, which is absurd since the state just becomes implicit).
(1) the raw system-level APIs are often not class-based, but nearly all frameworks above it are.
edit: the footnote broke formatting
- TsiCClawOfLight 6y agoClasses look familiar, but that's misleading because they don't actually work like one would expect. Hooks are actually much closer to what react does internally, and therefore behave much less surprisingly once you get into advanced things. Think of hooks like a missing primitive (similar to functions, classes and closures) that are critical to the react language, but don't actually exist in JavaScript and thus have to be approximated.
- moring 6y ago> Classes look familiar, but that's misleading because they don't actually work like one would expect. Can you give an example for that? So far, they worked pretty much as I expected. (Coming from a mostly Java background, which is relevant for what I expect).
- realharo 6y agoRegardless of how you feel about hooks, they are, or are quickly becoming, the new standard for most React code, so a React tutorial obviously needs to familiarize people with them. The current tutorial is good at explaining React concepts though, so making the jump to hooks after reading it and doing a few simple playground projects should be trivial. As for why people like them so much as opposed to classes, my guess would be that they let people more easily write reusable behaviors for common tasks (e.g. look at this one for integrating with RxJS Observables for projects using RxJS https://github.com/LeetCode-OpenSource/rxjs-hooks#examples https://github.com/LeetCode-OpenSource/rxjs-hooks#examples - with classes you would have to put code in several lifecycle methods, sometimes duplicate code, for each individual component).
- dynamite-ready 6y agoHooks are a fair bit easier to manage than classes in React. It won't change what you do with React much, but from file to file, it's more pleasant to read a hook based component, when compared to a class based component, once you're used to it. Hooks also simplify working with React context a great deal. If that's your option for state management.