6 ms·
I'll never understand the "hooks are bad" people. I don't find useEffect confusing, creating custom hooks is a breeze, life is so much better than old class com
by ervine 3y ago
I'll never understand the "hooks are bad" people. I don't find useEffect confusing, creating custom hooks is a breeze, life is so much better than old class components.
When I read complaints about hooks I'm either missing something fundamental about why they're bad, or the people complaining are missing it.
- artdigital 3y agoYeah I never found them difficult and they’re making my life so much easier Curious what specifically about them is difficult or hard to understand?
- danielvaughn 3y agoSome people find the mental model to be very confusing. I've been writing hooks for years so intuitively, the behavior has sunk in. But I still admit that it's mind bending to try and understand how it actually works. The original commenter is absolutely correct that it diverges from both functional and OOP, and while that's not necessarily a bad thing, it's reasonable for people to consider it a net negative.
- coolThingsFirst 3y agoWhats the beat resource for the mental model?
- danielvaughn 3y agoThere was an article I read, maybe 3-ish years ago, that explained it very well. It had you actually build a hook from scratch (not a custom hook, like actually implementing hook-like behavior into JavaScript). It was great. I just tried looking it up but couldn't find it, if anyone knows what I'm talking about please link here.
- WirelessGigabit 3y agoFollowing in care you remember / find it.
- jridgewell 3y agoAny chance it was https://pomb.us/build-your-own-react/ https://pomb.us/build-your-own-react/? You can't really have hooks without building an entire framework, because the framework has to have the preserve the execution context between renderings.
- brigadier132 3y ago"useEffect" itself is not what is confusing. What's confusing is when you have many dozens of components with effects all loading state asynchronously. Some of them might even be executing callbacks passed as props from parent components. Oh, and then those effects sometimes re-execute when the effects of parent components complete and pass down updated props! It actually reminds me of programming VHDL.
- andix 3y agoAnd that's why using effects for such tasks is a huge anti pattern. https://react.dev/learn/you-might-not-need-an-effect https://react.dev/learn/you-might-not-need-an-effect
- brigadier132 3y agoThe problem is you literally need to use effects to make api requests or access browser apis. You can't get away from it. Maybe you can try and tame the complexity by centralizing this logic as a small team but if the team gets big enough it's fruitless.
- ervine 3y agoYou're right, but I haven't touched a React project in ages that wasn't using a fetch library of some kind that give you hooks. Cause you're inevitably gonna want to track loading state, error state, refetch on error, revalidate etc etc. swr or tanstack-query are great and remove the need for useEffects for API requests. For sure they come with their own overhead, but there isn't getting away from the complexity of handling API requests and all the possible paths that can happen.
- andix 3y agoIsn't useSyncExternalStore the recommended hook for API requests? And for making API requests I think most people recommend using a library. React-query, RTK or swr for example. For accessing browser apis useEffects are the way to go, but those are usually extremely easy to understand in my experience. Or just use a library that provides the needed functionality.
- ramoz 3y agoFrom my experience (picked up react when it came out) hooks are not objectively better than class components in a standard web app. In the more reactive scenarios hooks are great, and your app is already complex and in its own world of mental models. Hooks enable complex patterns too easily and I think this causes a lot of breakdown in what would otherwise be simple applications.
- vinniepukh 3y agothe problem with useEffect is how much it gets used when it doesn’t have to be its overuse makes it evident that hooks (or React in general) is poorly understood
- lopatin 3y agoAs someone who was into higher order components but left front end work before hooks were a thing, I’m wondering how the programming experiences are different are different between the two. It seems to me like more or less the same thing but hooks have more magic? Is it just a syntactic difference or are hooks a paradigm shift even further than HOC?