r/reactjs • • 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?

44 Upvotes

36 comments sorted by

View all comments

Show parent comment

1

u/That_Kaleidoscope_23 Apr 18 '24

Using JS , you can create classes by using functions , no need the class keyword. Whenever I see some react project made mostly by uisng functional components, I generally see a lot of copy-paste repetitive code, and it is very hard to debug. It is hard to debug , because developers create a lot more classes using those functions , but those classes only hidden in the code . If that was a class component based project , developers tend to think before creating a class. Also , I think it's harder to debug a buggy project when the code is not well organized ,compared to a code where coder spent time thinking rather than finishing it fast as possible with lots of bugs.

2

u/That_Kaleidoscope_23 Apr 18 '24

just checked their repo , only the last commit.

https://github.com/facebook/react/blob/ea24427d16f3ac9b0f3bb45cdc7919ac208130c9/packages/react-reconciler/src/ReactFiberClassComponent.js

the code is a part of an experimental renderer feature. For some reason I do not know , they are using and defining classes in a functional way , but I do not see any benefit of creating a class by using Object.defineproperty like functions , rather than defining classes directly . I do not see any reason that approach would be better than defining classes and let the typescript compiler or a JS code optimizer to handle that and optimize the code where it sees a class keyword. I hope react team has some good reason to deprecate class components , but I'm not convinced yet.