3 ms·
> 1) classes and inheritance don’t belong in JS You gonna back this claim up? Because it doesn't stand on its own. Inheritance certainly doesn't gel with _Reac
by viklove 7y ago
> 1) classes and inheritance don’t belong in JS
You gonna back this claim up? Because it doesn't stand on its own. Inheritance certainly doesn't gel with _React_ (use composition), but to say it doesn't belong in JS is rather naive.
> 2) they allow for more code reuse
You can already get just as much reuse out of React's context API
> 3) they allow you to write simpler, more declarative code.
This is rather subjective, but I'd argue it's not simpler, because it "hides" the state with internal magic, and it's not even more declarative because using a context (or composition) based approach would be just as declarative.
I'd actually argue it's far more complex, because each invocation of `useEffect` doesn't even have the same result! There's some behind-the-scenes magic going on that makes it harder to reason about and find bugs.
The nice thing about having classes is that it's very easy to understand what code runs when a component is initialized (constructor and componentDidMount) versus what code is run on each render cycle (the render method). Hooks just removes that whole distinction and throws everything into one place. Devs who are new to React will definitely struggle with understanding what is going on behind the scenes.