|
|
|
|
|
by csande17
2055 days ago
|
|
One thing that feels off about hooks to me is the "primitive" ones React provides. Like, here are the basic things that hooks can do: 1. Tell the component to re-render 2. Store an object that persists between component renders 3. Run something after the component has been rendered 4. Run something when the component unmounts React doesn't give you functions that do these operations individually. Instead, you get weird hybrids like useState, which combines storing a value with telling the component to rerender when the value changes. So you sometimes have to jump through hoops to get the behavior you want. (Well, useRef sort of does (2), except it constructs the object itself.) |
|
Components are functions of their props and state, returning markup (in the form of a descriptor foe the rendering engine). When a prop or piece of state changes, the component must re-render. The fact that useState might be responsible isn’t relevant.
That said, I’m also still struggling with hooks (and I like functional programming in JS). I want to like that hooks let you wrap your domain logic into neat bundles, but I find that I miss the lifecycle callbacks of the older class based style.
Hooks based components are trying to make modules that are “about” the problem domain they illustrate, but they do it as a feature of React. React isn’t about your problem domain, it’s about driving the DOM and rendering, and I think it makes more sense to treat React code that way.
I’m open to having my mind changed on this. I have spent way more time writing classical React than hooks-based.