r/reactjs • • Dec 25 '19
Is it still worth learning about class components if you are just getting into React?
[deleted] — view deleted post
92
Upvotes
r/reactjs • • Dec 25 '19
[deleted] — view deleted post
19
u/chaoszcat Dec 25 '19 edited Dec 25 '19
I started in class components, few months before they introduced hooks. Now I'm full time hooks/functional user since I find that functions are a little bit cleaner and easier to write, and no
bindcalls needed for events, and everything in one function make it easier to share variables too without scattering variables everywhere.Both are legit, but I prefer function and hooks.
102
u/NoNeedToBail Dec 25 '19
I'd second that learning both is pretty worthwhile - it's likely that any production codebase you work on will contain both and you'll need to maintain/change both types of components.
6
u/[deleted] Dec 25 '19
Yes. Because the chances of you seeing old code is going to happen. Period. Just because something is the new hotness now, doesnt mean that every codebase is instantly updated. Always learn the old ways + the current. Then as it comes out, always learn what's up next. Most of our jobs as developers is learning.
5
u/Rawrplus Dec 25 '19 edited Dec 25 '19
Yes and no.
Not for your personal reasons. Functions with hooks are the way to go and will cover 99% of your usecases.
The reason why I'm saying you should cover at least the basics of class components (state, constructor, lifecycle methods) is because a lot of older tutorials are written using class components and a lot of projects you might work on have not been transitioned to function components (especially since even Dan himself suggested, to use function components on new projects instead of transpiling old ones). I've recently joined a start up with few already up and running projects. The 2 I got to work on are written with state components. Not because we'd want it, the project ceo himself admitted it's suboptimal, but it's because refactoring huge projects takes a lot of manhours that we need to spend on implementing features and fixing bugs instead. So now I'm super glad I took time to learn state components (although to be fair, back when I started learning React hooks were not even out)
So it's worthwhile knowing what the thing does at the very least. It's the time investment of 2 days that will save you countless years of confusion.
5
u/commanderCousland Dec 25 '19
It depends on your immediate goals.
I've often seen beginners getting all kinds of confused on the lifecycle components if they don't have much of prior programming experience.
When i teach or advise i start with hooks.
But that being said, most tutorials and general feature specific content online is still based on classes.
A special mention of the error boundary component that's still recommended to he done as a class component.
So knowing and understanding class components is definitely worth the effort. If you're set on using react and the many community creations that come with it you need to be able to understand and implement things in classes or in certain cases - convert to hooks from classes if you're so inclined.
TL;DR: Start with hooks. But don't skip on classes if you intend to get max benefit from the tech and the community.
2
u/drink_with_me_to_day Dec 25 '19
Anything complex with lifecycle functions is shit to do with hooks
4
u/a-c-sreedhar-reddy Dec 25 '19
If you have to deal with old codebase then you need to have some knowledge of class components
6
u/striedinger Dec 25 '19
You should probably learn class components to get familiar with reacts life cycle methods, and then switch to functional components.
11
u/qomu Dec 25 '19
You should learn both because people use both. I think class components are quite a bit easier to read, especially if you know the component requires state beforehand.
3
u/Faheemify Dec 25 '19
Class components and then hooks.
Classes are not limited to ReactJS. It is a part of JS and will really help you with other things. I think using Class components is still fine.
You should try both and use the one which fits good in different use cases.
5
u/TheActualStudy Dec 25 '19
Class components are very clear about what they do, when they do it, and provide a good means of illustrating to your team how component lifecycle should be handled explicitly. That said, almost everything I write now is functional components because they haven't really come up short. I would probably say look into class components if you encounter lifecycle shortcomings in functional components or you're working on a project (or a team) that makes use of class components.
4
u/strangescript Dec 25 '19
Most people are saying yes but if you are not going to work with older code then don't bother. I have started using hooks and not looked back and they are the future of the framework.
3
u/skyboyer007 Dec 25 '19
better start from hooks. you know I heard quite often that while learning hooks after experience with classes you have to forget and start from the scratch. life cycle methods are way more straightforward(in cost is boilerplate code) and the only important thing for class component is binding
thisMeanwhile hooks use closures everywhere so there are more cases for "oh, snap".2
u/[deleted] Dec 25 '19
Start with hooks.
2
u/popc0ne Dec 25 '19
Yes ... people who claim they only use hooks must not build very complicated components... classes are still very important and some things you simply can’t do with hooks.
1
u/Cryp71c Dec 25 '19
We use almost exclusively class components for their tightly controlled performance and improved readability. Definitely worth learning them.
1
u/pacman326 Dec 25 '19
I’d say know both. But that also being said you should focus on building things right now. I will mention that I do believe while class based implementations aren’t going anywhere it’s entirely possible in the mid to late future class based implementations won’t be as easily portable. Notice that in React 17 some lifecycle methods are being fully deprecated.
1
u/Bongster7 Dec 25 '19
I'd suggest starting with class components, even to just get a basic understanding.
I started with React the week following hooks official release. We were using hooks in our webapp but I did learn class components first (as there were more article, courses and tutorials out there) which has come in usual when you're trying to reverse engineer a class components functionality (mainly from a stackoverflow answer) for your functional component.
Don't think it's absolutely necessary but spending a few hours getting to grips with class components won't hurt.
Have fun 😊
1
u/R3PTILIA Dec 25 '19
Kind of, id advice to read the documentation about classes to have a good general knowledge, but I haven't used a class component every since using hooks for the first time
1
u/fistyit Dec 25 '19
Learn both my friend. they're both pretty simple and you wouldn't want to miss out on anything
1
u/[deleted] Dec 25 '19
Absolutely
1
u/skidmark_zuckerberg Dec 25 '19
You will without a doubt see class components in a production code base. You need to know the lifecycle methods, how they work, and when they are needed. I honestly can’t imagine a React dev not knowing how to use Class Components, at least not anytime soon. In a technical interview for a React position, you will be asked about lifecycle methods and how they work or what they are for.
I’ve slowly been introducing React Hooks into some of our production code bases, but 95% of our code base is still relying on Class Components. You wouldn’t be able to do anything with those code bases without knowing how to use CC’s.
1
u/earthboundkid Dec 25 '19
Are you worried that if you learn too much your head will explode?
1
u/sunny_lts Dec 25 '19
I'm still doing class components, they're pretty, better structured, convey more about the component. I just really like them, that's React for me
1
u/Tolgeros Dec 26 '19
I'll say this: hooks are not overrated, but classes are underrated. Learn both.
1
u/george4517 Dec 26 '19
I’d say yes because it’s likely that you’ll come across code written prior to hooks that hasn’t been rewritten with hooks. I would learn hooks first however
1
u/[deleted] Jan 17 '20
If you ever plan to work with react in a professional setting you should probably read up on them as old code for react will be all class-based.
1
u/tarunspartan Dec 25 '19
Learn Class Components
Learn Life Cycle Methods
Learn Functional Components
And Hooks 🎉
1
u/xjaak Dec 25 '19
The only React code I've ever written is based on functional components but I've still spent a bit of my time reading up about class components and lifecycle methods. Imo you don't really *need* to learn and write class-based React but it's a good thing to be aware of.
1
u/[deleted] Dec 25 '19
Ever since hooks came out, I've never used class components again in my job. People always talk about that one niche case, which hooks might not perfectly cover, but in practice it really never happens...
You just have to learn about class components if you are gonna be working with older code that might still contain them, but if you only write fresh code I would skip learning about class components.
1
u/KevinVandy656 Dec 25 '19
I'd say yes, because a big part of my job right now is converting old class components into functional ones using hooks. I needed to understand how to remake the same behavior.
1
u/MrBr7 Dec 25 '19
You are looking it the wrong way. Learn about React concept, paradigm, data flow.
Class components, functional components, HOCs and everything else is just a tool to achieve something.
Once you understand how to structure things, everything else will be logical.
0
u/helping083 Dec 25 '19
Class components are important. In my opinion pages should be the class components.
0
u/AlienSoldier Dec 25 '19
There are still life cycles that don’t exists hooks (componentDidCatch for example) So.. yeah maybe you should learn what isnt exist yet in hooks so you could decide for yourself
0
u/ryan_solid Dec 25 '19
Start with Hooks. While harder to say reverse engineer the paradigm is more simplistic conceptually. All the lifecycle stuff can come later as the need arrives.
-1
u/devtoolss Dec 25 '19
Stop being lazy
0
u/[deleted] Dec 25 '19
[deleted] — view deleted comment
0
u/[deleted] Dec 25 '19
[deleted] — view deleted comment
0
u/[deleted] Dec 25 '19
[deleted] — view deleted comment