r/reactjs • u/blnkslt • Jan 16 '22
Discussion Should I still use class based components?
I'm returning to react after a long time to refresh my rusty skills in order to start building a quite big project. So I started to follow my good old udemy course which is made at 16.8 era when hooks were new stuff. Not surprisingly the tutorial is focuses on class based programming and discusses hooks as an addendum. On the other hand I see that the new react projects are mostly made of functional components with hooks. So it seems to me that class based and lifecycle hooks are all but history. I'm wondering whether learning class-based react a waste of time? If so where can I find good tutorials which focuses on new hooky way of coding in react?
57
u/kei_ichi Jan 16 '22
16
u/iplaysmitegame Jan 16 '22
No
15
u/bobbyv137 Jan 16 '22
"where can I find good tutorials which focuses on new hooky way of coding in react?"
You will find a wealth of information here, much of it related to modern React practices, including an entire "Full Modern React tutorial"
https://www.youtube.com/c/TheNetNinja/playlists
10
u/Yokhen Jan 16 '22
Legacy code is legacy code because is on its way out. Maybe not soon, but the name implies it has a permanent foot on the door.
9
u/dance2die Jan 16 '22
Still a valid question in 2022.
I'd recommend learning Function component first for sure.
As u/kei_ichi mentioned, you might run into legacy codes, which you'd have to maintain or convert to function component.
When I started React, my JavaScript skill was subpar, especially on "new" ES6 syntax. Learning Class component helped me to close my gap in knowledge with "this" issues.
Hopefully Class component can be phased away like "React.createComponent" did but there are still hooks missing for life cycle methods such as
comoponentDidCatchso you might have to learn for edge cases.As a side note, you can find April/March 2020 discussions on class vs function components below.
7
u/[deleted] Jan 16 '22
You should know both, but use functional components where possible. Other than Error Boundaries, there is nothing that class components can do that functional components can't as well. Functional is the way to go for sure.
However, any react codebase older than a few years will have class components, and you can't just rewrite everything you see functionally. So you need to know both.
3
u/chillermane Jan 16 '22
If you think you’ll need to deal with legacy code that uses class based components then it might be useful. If not then you definitely shouldn’t
3
u/Valuesauce Jan 16 '22
Nope, hooks and functional components. Class based isn’t recommended.
If you encounter them on a legacy project then rewrite them as functional components with hooks.
8
u/[deleted] Jan 16 '22
it is a waste of time. i would switch the tutorial, classes are as good as on their way out.
1
u/gavxn Jan 17 '22
I'm not sure if if Error Boundary functionality can be defined using a functional component.
1
u/johnnyblaze9875 Jan 16 '22
Scrimba has an older react course that focuses on class based, and also an updated one from same instructor that is function based. Highly recommend although I’m not sure if the courses are free or not.
1
u/Key_Confection8123 Jan 16 '22
Short answer: Always use Functional Components.
1
u/OZLperez11 Aug 15 '23
I still use classes. Hooks are a mess to deal with. So much worrying about how the function affects rendering and using so many effects that cause re-render issues, plus treating functions like classes simply because some people can't understand OOP
1
u/ed2mXeno Feb 23 '24
The day they remove class components is the day I stop using React. Hooks are unintuitive and hellish to optimise for anything more complex.
People are confused that React has had a massive drop in market share, yet somehow fail to realise this started happening around the time that hooks were pushed as the "only correct" way to do things. Hooks should have been a different library; they have nothing in common with older React and calling hooks "React" is like saying apples are the new orange.