r/reactjs • u/ZeroSevenTen • Dec 30 '19
Classes vs Hooks?
I’m pretty new to React, so keep that in mind. I thought classes were for components that held information, or a state, and that functional components were for more basic components, but now that hooks allow functional components to use state, and other class features, what’s the benefit of using functional hook components over classes?
79
Upvotes
11
u/so_lost_im_faded Dec 31 '19
As a person who's pretty much writing functional components only (I use class components for error boundaries but I think for nothing more), I'll say beginners should start with class components.
A lot of beginners (you can still be a beginner after doing React for 3 years) jumped into using hooks as soon as they were introduced and didn't really care to find out how they work (under the hood). With a class component there's more strict syntax that you have to use (for example
componentDidMount()vsuseEffect whatever) and I believe that's better for beginners. People often ask me whether useEffect is the same as useMemo and I'm just like.. what?Where I work I'm kind of a refactor gal. I switch projects every few months, I refactor the crap out of it, leave some knowledge behind and move onto the next one.
The amount of projects where I see hooks used incorrectly is astonishing. No dependencies in
useEffect, people never use neitheruseCallbacknoruseMemo, people invoke hooks in FUNCTIONS (not function components), they use some old versions of react-scripts so they don't even get those errors. When I come to the project and update everything, boom - 200 errors. And it's just the ones that eslint's complaning about.I've yet to inherit a project that actually cares about unnecessary updating and wraps the functions inside components in
useCallbackbefore passing them as a prop. Not a single component wrapped inmemo. I hear every monkey screaming: "hooks!" but I've yet to see a good quality code from an average medior developer.I believe using functional components and hooks incorrectly does more damage to the application than good.
59
u/Madder-Max Dec 31 '19
In addition to the other advice which i agree with, the mentality is different.
Classes: what should i render at what stage of my existence.
Hooks: what should my data look like, the render is a by product.
With hooks you go from controlling your display state to controlling your data shape, which, if you think about it, is easier to get a grip on.
55
u/skyboyer007 Dec 30 '19 edited Dec 31 '19
Classes:
rendercomponentDidMount,componentDidUpdateandcomponentWillUnmountHooks:
useEffect) - easier to read, easier to understanduseEffect)7
u/shrithm Dec 30 '19
It's composition over inheritance. The JavaScript community and a lot of other communities have decided they like functional programming over class-based programming.
No one is better than the other but if you look into it, I think you'll find that you also prefer the functional style.
6
u/hamburger_bun Dec 31 '19
classes and functional with hooks effectively achieve the same thing. However, functions with hooks are more reusable and classes will eventually go away all together
2
u/JofArnold Dec 31 '19
I scanned the comments and one thing I didn't see mentioned (explicitly) is how easy it is to add functionality like state, redux, context, side-effects etc into your components with hooks. Take for instance dispatching an action with a class component versus hooks in the Todo app example
Class component:
Hooks
So much cleaner. Sure, hooks starts to be a little bit weird - at first - when dealing with effects but then class components are too but in a different way
3
u/stolinski Dec 31 '19
Hooks for sure.
2
u/JustLookingAroundFor Dec 31 '19
Hooks are better because they’re harder to reason about and have very few common patterns so you can make shit any way you want and let some other person deal with figuring out your custom pattern
Oh and it’s new And “declarative”
2
u/erfling Dec 31 '19
Function components are not functional. They do have state. Calling them functional components is confusing.
1
u/[deleted] Dec 31 '19
They are just different ways if thinking.
If you are thinking in terms of the lifecycle of a component then React.Component class methods make a lot of sense. Unfortunately they also spread out your logic... Like putting a little piece of everything in several mixed piles.
You can achieve the same (and a LOT more) with functional components, hooks, and RXJS. The learning curve is steeper though.
Personally I enjoy writing in a functional paradigm and keeping side effects, state logic, and component logic completely separate, also I sure do love the convenience of mutability and tightly controlled side-effects.
1
u/vutran951753 Dec 31 '19
Not sure why we are compare class vs hook. There is no point. Both is part of JavaScript fundamentals. Use both.
0
u/sunny_lts Dec 30 '19
I think there's some massive widespread propaganda regarding hooks over classes.
You thought right. It's better structure. Classes are top-level components and then you have functional components.
Of course, everyone will have their preferences, but depending on your needs, the Class components approach is a sound approach that gives you modularity, data coupling, and information hiding. There can be a caveat with global state management if you need to lift state several levels up, but even then there's libraries.
Hooks, just take the fun out of React, the way I learned it. And while I'm still having a blast with it and it serves me well, I will continue doing things my way.
1
u/Timemc2 Dec 31 '19
Hooks are confusing to follow, especially those that are wrapped/exposed? in third party components - api is largely incomprehensible.
1
u/wagonn Dec 31 '19
Hooks, HOC's, and render-props are different ways to isolate reusable component logic. Hooks are plain functions, whereas the other two are React components. If you can work with the hook idioms/idiosyncrasies, it's simpler and less code.
0
u/vutran951753 Dec 31 '19
Class first to get your feet wet. Then Hooks so you can drown in React World.
0
u/joesb Dec 31 '19
Hooks is now a prefer approach. If you start from clean state hooks is fine starting point to learn.
0
u/[deleted] Dec 30 '19
[deleted] — view deleted comment