Class Components vs Functional Components in React (Which is better? - Beginner's Guide)

🚨 Join the world’s BEST developer community “Zero to Full Stack Hero”: www.papareact.com/course
📩 Want coding problems (with solutions!) delivered to your inbox daily? www.papareact.com/dailycoding...
This video is designed to get you familiar with both Functional components & Class based components where we cover topics such as:
- Why it's critical to learn both to grow as a developer
- Which to use when writing new components
- How to refactor a class based component to a functional component
- Hacks, Tips & Tricks to know about to help you out
- Key differences between the two with State & Props examples
🛠️ LOOKING FOR THE CODE?
links.papareact.com/github
🎙️ PODCAST
links.papareact.com/podcast
🖥️ CODE
Get the code for my builds here: links.papareact.com/github
🌍 SOCIALS:
Instagram: links.papareact.com/instagram
Facebook: links.papareact.com/facebook
LinkedIn: links.papareact.com/linkedin
Twitter: links.papareact.com/twitter
Discord: links.papareact.com/discord
Newsletter: links.papareact.com/newsletter
💰 WANT TO SUPPORT THE CHANNEL?
Donate here: links.papareact.com/donate
Grab some PAPA Merch: links.papareact.com/merch
🕐 TIMESTAMPS:
00:00 Intro
00:16 Skillshare Sponsorship
02:44 Functional Component Layout
03:36 Class Based Component Layout
04:32 Functional Component Explained
05:58 Functional Component vs Class Component Features
08:26 Changing Class to Functional Based Component
09:53 Using State in Class and Functional Components
12:53 Using Props in Class and Functional Components
15:32 Outro
Let’s get it PAPAFAM 🔥.
#reactjs #javascript #tutorial #classcomponents #functioncomponents #react

Пікірлер: 193

  • @SonnySangha
    @SonnySangha Жыл бұрын

    📬Do you want DAILY coding problems sent DIRECTLY to your Inbox? (with solutions the next day) 👉 links.papareact.com/university

  • @serenity-symphony
    @serenity-symphony2 жыл бұрын

    Sony, you have mentored me in this career. highly appreciate you.

  • @meetshah4432
    @meetshah44322 жыл бұрын

    For almost a year now, I have never used class components. I just don't feel the need to use them anywhere. Functional components can do everything now that a class component can do. Also React's new docs will also replace all class components with functional.

  • @TheSoulCrisis

    @TheSoulCrisis

    Жыл бұрын

    Yeah functional components seems to be the future to me. I have a buddy who works for a private tech company and he usually does functional components now.

  • @lancetakiguchi9756
    @lancetakiguchi97562 жыл бұрын

    Really great video. That last bit of syntax, of pulling out the props, and still having that catch all props at the ends seems really clean

  • @codeWimran
    @codeWimran2 жыл бұрын

    Always love and enjoy your contents, thanks for everything 🙏

  • Жыл бұрын

    An explanation if there is a difference in the re-renders between the two approaches would be nice.

  • @isaacidibia2117
    @isaacidibia21172 жыл бұрын

    Very happy to come across this, i have been struggling to understand this, but now i do well a lot. Thanks

  • @14Shlomid
    @14Shlomid Жыл бұрын

    Thank you for this Video Sir. very clear, professional and helpful. so in 2023 can you suggest for new projects to use FC as the default preffered?

  • @shreyasp4789
    @shreyasp47892 жыл бұрын

    Functional based components is a winner. If you want to use class based components instead of going to react they can go for angular or vue

  • @alexkonstantinov4256

    @alexkonstantinov4256

    2 жыл бұрын

    Agree, I have never seen a scenario where a class component can be useful after the introduction of hooks.

  • @munandisichali605

    @munandisichali605

    2 жыл бұрын

    I agree but for more information there are no classes in vue lol

  • @lucasrodrigues9766

    @lucasrodrigues9766

    2 жыл бұрын

    But you have to learn it since you will encounter projects that you will have to deal with that use class components

  • @alexkonstantinov4256

    @alexkonstantinov4256

    2 жыл бұрын

    @@lucasrodrigues9766 man, we should know class components for sure to work with legacy projects. But please don’t use class components in new projects.

  • @mrcrypticxdev

    @mrcrypticxdev

    2 жыл бұрын

    Agreed!

  • @alamesawikanningkaton5028
    @alamesawikanningkaton50282 жыл бұрын

    Glad to see this video, i just learned react as well and confusing about which better between class and functional component. Thanks a lot sonny 😁

  • @saitejagatadi9711
    @saitejagatadi97112 жыл бұрын

    Sonny, Could you please make a video on time management and how to avoid distractions. You are so great and master in time management.

  • @r_yan3531
    @r_yan3531 Жыл бұрын

    Now this is how you teach anything 👍 Clearing fundamentals first and then diving into the project making The right way.

  • @mounasellami199
    @mounasellami1992 жыл бұрын

    Thank u about this gorgeous explanation Sonny, I have question! When we will be more deep in development we use Higher Order Component ?

  • @feliwein_cc
    @feliwein_cc2 жыл бұрын

    fire content as always, could you make a shortcut vscode video? 11:55 kinda nice. hitting that 100k papa react! that seems like a pretty good christmas

  • @dimitritsikaridze6220

    @dimitritsikaridze6220

    2 жыл бұрын

    ctrl D

  • @salvatorebarretta1410
    @salvatorebarretta1410 Жыл бұрын

    u're so clean! i've learned a lot of information so quick thanks!

  • @jaygarricktheflash
    @jaygarricktheflash2 жыл бұрын

    Wow! Been a bit since I watched your vids. Love the quality upgrades to the transitions and overall viewing experience. Job well done.

  • @Vikingsplayer
    @Vikingsplayer Жыл бұрын

    this is an fantastic video sir its clears some questions of class based components thanks

  • @chrisbrowning550
    @chrisbrowning550 Жыл бұрын

    Great video and nicely explained. Thank you !

  • @guitarupfront9444
    @guitarupfront94442 жыл бұрын

    i used your link just to support you I love your content and appreciate you

  • @codekicks2171
    @codekicks21712 жыл бұрын

    Love you bro from India, You have made this topic very easy for us, Thanks a lot

  • @mthoNtanzi
    @mthoNtanzi9 ай бұрын

    This is very helpful information. Really well put together.

  • @md.mehedihasan800
    @md.mehedihasan8002 жыл бұрын

    I am from Bangladesh, one of the greatest programmers of all time. I like your projects very much.

  • @mayurbahuguna8438
    @mayurbahuguna84382 жыл бұрын

    Man you are amazing 💫 love you brother...

  • @empty_dayofthetech
    @empty_dayofthetech Жыл бұрын

    What program do you use to draw on the screen while recording?

  • @mierLG
    @mierLG2 жыл бұрын

    Thank you so much for a very detailed explanation, I fully understand the difference between the two.

  • @DevlogBill
    @DevlogBill Жыл бұрын

    Hi Sonny, just found your channel by accident and you have excellent content. I just began learning React 5 days ago and I am picking it up quickly since it is functional. Before picking up React I was studying hard classes and found learning classes and creating new objects kind of confusing especially when it came to learning how to use this key word, I prefer React hooks any day of the week! Question? I am not in the tech industry yet. I am still fairly new to learning front end development. I was wondering from your personal experience, are most companies using React the new and modern way in a functional format or are most using React as a class-based system? After I get a good handle of React as function would you recommend I re learn React again but in the old way which was classes? Thanks Sonny!

  • @Andres_cali

    @Andres_cali

    Жыл бұрын

    Uuu and he doesn't answer

  • @DevlogBill

    @DevlogBill

    Жыл бұрын

    @@Andres_cali LOL I am pretty sure he got soooo many subscribers to attend too.

  • @alwyschwuetz
    @alwyschwuetz8 ай бұрын

    And that is how I understood functional and class component in less than 16 mins guys, of course after more than a few months of learning react

  • @themoadel
    @themoadel2 жыл бұрын

    love it man really you're an added value

  • @Luca-re3ve
    @Luca-re3ve Жыл бұрын

    how can u write on the screen whidout using paint or similar? that would be so usefull to know

  • @mik0son
    @mik0son Жыл бұрын

    Dude is one of the best dev teachers there is!

  • @RizaHariati
    @RizaHariati2 жыл бұрын

    I never use class components since I first learn React last year. But a lot of more senior programmer do still use the class components so I have to learn it too. But it's to 'noisy' for me. This video confirms it. Thank you ^_^

  • @aryandhanawade8063

    @aryandhanawade8063

    Жыл бұрын

    React is anti-Islam !!!

  • @learningandplaying262
    @learningandplaying262 Жыл бұрын

    i first learned react and i am coding on class component, because i like coding on class component and it makes me easy to code maybe i have not coded in functional component before

  • @soumyanilpodder8435
    @soumyanilpodder84352 жыл бұрын

    Really good video! Very helpful...❣️

  • @frankhenry10x
    @frankhenry10x2 жыл бұрын

    Sonny is ZTFSH still live/ open for Dec?

  • @danuvian
    @danuvian Жыл бұрын

    Great video. It was explained very well.

  • @pulserudeus7968
    @pulserudeus79682 жыл бұрын

    precise explanation🔥

  • @thehomeschooldev6126
    @thehomeschooldev6126 Жыл бұрын

    Thank you. Very well taught.

  • @sanTakagi-gv4dy
    @sanTakagi-gv4dy7 ай бұрын

    Explain very clear, thanks.

  • @mikedelta658
    @mikedelta658 Жыл бұрын

    The one and ONLY Sonny! Thank you being my energy!!!

  • @ashu_gaming_and_vlog
    @ashu_gaming_and_vlog2 жыл бұрын

    How do you write or draw something in code name of that apps please.

  • @zhrgci
    @zhrgci2 жыл бұрын

    Useful af, since I was following a tutorial that still used class components (while a guy on reddit said functional comp's were better)

  • @inspirationsemotivationtak1467

    @inspirationsemotivationtak1467

    Жыл бұрын

    Hey can you name the channel for class based components in react

  • @zhrgci

    @zhrgci

    Жыл бұрын

    @@inspirationsemotivationtak1467 @Almighty java

  • @DanyTLaw
    @DanyTLaw2 жыл бұрын

    Thanks for the free month and great content. You are now my go to place when I eat lol

  • @aengtech
    @aengtech2 жыл бұрын

    I had an argument on twitter. This video helps a lot. Thanks

  • @salilchandwadkar789
    @salilchandwadkar7892 жыл бұрын

    Awesome 🔥🔥🔥

  • @isurujn
    @isurujn2 жыл бұрын

    Hello, what's the tool you use to draw on screen btw? Looks cool.

  • @eriknilsson4994

    @eriknilsson4994

    Жыл бұрын

    I think I found the program he's using. It's called ScreenBrush

  • @jealousy0890
    @jealousy08902 жыл бұрын

    Sonny, would love to see something like a dropbox type clone, so users sign up with their own emails instead of google sign in and can upload and share files!

  • @trungphan2504
    @trungphan25042 жыл бұрын

    Nice video bro, thanks you so much

  • @manjunathamj3452
    @manjunathamj34522 жыл бұрын

    Navigation 6 using class based component any suggestions

  • @sambitmohanty8308
    @sambitmohanty83082 жыл бұрын

    Doubt cleared Thank u

  • @JayroJunior
    @JayroJunior2 жыл бұрын

    Very good content, thanks!

  • @osvaldogalvez4776
    @osvaldogalvez4776 Жыл бұрын

    Thank you for the videos !

  • @rohandeysarkar8443
    @rohandeysarkar84432 жыл бұрын

    Watched this channel grow from 5k to 100k. Way to go Sonny 🔥🔥

  • @andyb9990
    @andyb99902 жыл бұрын

    Sonny could you do a React Testing video?

  • @subramanyashegade8248
    @subramanyashegade82482 жыл бұрын

    Thumbnail is 🔥🔥

  • @CavanMitchell
    @CavanMitchell2 жыл бұрын

    wow, got a lot of clarity from this vid

  • @zongyu100
    @zongyu1002 жыл бұрын

    Hey guys anyone knows which brand is his headset? it looks really nice

  • @SonnySangha

    @SonnySangha

    2 жыл бұрын

    Audiotechnika!

  • @faikyesilyaprak8761
    @faikyesilyaprak87612 жыл бұрын

    Clean and pure information. Quite rare in 2021. Thanks for the great effort. Keep it up!!

  • @JustClickApprove92
    @JustClickApprove922 жыл бұрын

    I think it’s good to be aware of class based components, and understand how they work, but I can’t envisage a scope for using them over functional components. Currently in my job, I’m refactoring a react-native app written in class based components to functional components

  • @kaunghtethein3322

    @kaunghtethein3322

    2 жыл бұрын

    that looks like so much work

  • @JustClickApprove92

    @JustClickApprove92

    2 жыл бұрын

    @@kaunghtethein3322 yeah, it’s not fun 😭

  • @Saradomin65

    @Saradomin65

    2 жыл бұрын

    Refactoring a code is one of the most tiring thing to do in a project lmao. You have to like really be careful that all other functionalities are still working.

  • @CodingFire
    @CodingFire2 жыл бұрын

    100k Subscribers LETS GO!!! 🔥🔥🔥

  • @johnjohnix
    @johnjohnix2 жыл бұрын

    I still love using class, it is much easier when passing and updating state in a big scale project. function component is so dirty when working in a big project. Updating state from child is pain in the ass using function. Using class, you can just pass the state and you can update that state directly from child component without passing the state setter. "OR maybe I'm just missing something?"

  • @0080437

    @0080437

    2 жыл бұрын

    if you want, you can write all your states into "const [state,setState]=useState({all_your_state})" and pass only state and setState to the child component. though I am not sure if this is a good practice.

  • @raivikas200
    @raivikas2002 жыл бұрын

    100K is near 🔥🔥🔥🔥.

  • @toms_tech
    @toms_tech2 жыл бұрын

    A lot of Udemy courses are outdated and still teach using Class components =/ Any good course recommendations that teach Function components and decent projects?

  • @antoniofiorillo1380

    @antoniofiorillo1380

    2 жыл бұрын

    Tommy I suggest you "React -The complete guide" by Academind (Maximilian Schwarzüller). It teaches you function components and react hooks, also using typescript

  • @SonnySangha

    @SonnySangha

    2 жыл бұрын

    Dude check my course at PapaReact.com We got you

  • @toms_tech

    @toms_tech

    2 жыл бұрын

    @@antoniofiorillo1380 Thanks I actually bought and started that course, but I'm not the biggest fan of the teaching style and the projects look so ugly in comparison to other courses (Colt Steele's for example) lol. Colt's course is unfortunately dated and uses classes though :)

  • @naveenkumarkota2090
    @naveenkumarkota20902 жыл бұрын

    understood thank you!!

  • @ashutoshpandey4171
    @ashutoshpandey41712 жыл бұрын

    1 million subscribers coming soon..., keep teaching sonny :)

  • @TyMeadorMusic
    @TyMeadorMusic Жыл бұрын

    I write everything as class components. IDK why, but the component life cycle is more clear and makes more sense to me in a class than in a function.

  • @ayasalem1745
    @ayasalem1745 Жыл бұрын

    Bro took glasses off and i was like wow new youtuber nice

  • @yuno3364
    @yuno33642 жыл бұрын

    lol i was just wondering if someone would make a video about this. thanks

  • @kirakira160
    @kirakira1602 жыл бұрын

    Sonny the best!💪💪💪

  • @user-ob2cp1fd2f
    @user-ob2cp1fd2f Жыл бұрын

    Good explanation, but I still don't know in which cases developer supposed to use class instead functional components (Maybe I've lost something)

  • @The-Dev-Ninja
    @The-Dev-Ninja2 жыл бұрын

    thank you!

  • @JC-rv2tq
    @JC-rv2tq Жыл бұрын

    Love this

  • @rajeshwariy869
    @rajeshwariy869 Жыл бұрын

    Understand fully nice video make more video bro

  • @XXX1234ABC
    @XXX1234ABC2 жыл бұрын

    11:56 HOW DID HE REPLACE ALL THE STRINGS AT ONCE? SOMEONE PLZ TELL ME THIS SHORTCUT

  • @RoCkBiGdAvE

    @RoCkBiGdAvE

    2 жыл бұрын

    In vscode if you press ALT or OPTION then click somewhere it’ll add another cursor. You can also click and drag to make multiple selections. 👍 There are keyboard shortcuts too but this will get you off the ground. 😀

  • @zafariqbal92
    @zafariqbal92 Жыл бұрын

    Thank You!

  • @arthurweasley5857
    @arthurweasley58572 жыл бұрын

    its almost 2022, why would you still use class components? well sometimes you need to but thats just an expection

  • @anonymousgerman2809
    @anonymousgerman2809 Жыл бұрын

    great video man

  • @bobobobbyboy
    @bobobobbyboy2 жыл бұрын

    So there’s no other difference other than brevity and cleanliness?

  • @rl6382
    @rl638210 ай бұрын

    Im confused what happened at the end with the ..props, why did you add year as well? Where are you pulling those values from?

  • @darshanakv4946
    @darshanakv4946 Жыл бұрын

    Best tutorial!

  • @reach5593
    @reach5593 Жыл бұрын

    Functional components since react hook came. It makes my life easier.

  • @haryschampultra
    @haryschampultra2 жыл бұрын

    Thanks bhai!

  • @mikaelmodig5131
    @mikaelmodig51312 жыл бұрын

    In the end of class era, you did not really use constructor and super. You just declared state as an object inside your class.

  • @Victor-wh9bs
    @Victor-wh9bs2 жыл бұрын

    Good Work #PAPAFAM

  • @ikamy
    @ikamy Жыл бұрын

    That was helpful

  • @aryan7069_
    @aryan7069_2 жыл бұрын

    Great video

  • @rogerteku2877
    @rogerteku287710 ай бұрын

    Use effect in functional component while cycle in class components

  • @riteshkhodiyar184
    @riteshkhodiyar184 Жыл бұрын

    You explain nicely. Just a suggestion while speaking drop the accent. for example I cant really understand when you say could and couldnt . both sound same .suggestion As non native english speaker .

  • @snssatyabhagavan
    @snssatyabhagavan2 жыл бұрын

    Nice video 👍

  • @riz4691
    @riz46912 жыл бұрын

    Functional component is fun to use.

  • @KILLCHRISU
    @KILLCHRISU Жыл бұрын

    Surprised I wasn’t subscribed to you earlier. Nicely presented and explained very clearly thanks bro 👊 P.S. you should drop an affiliate link to where we can buy those headphones 😉

  • @SonnySangha

    @SonnySangha

    Жыл бұрын

    Consider it done dude!! I’ll add some - also thanks for being awesome!!!

  • @specy_
    @specy_2 жыл бұрын

    This doesn't really explain the advantages and disadvantages of the each. I feel like if you have a component that has some complex state or has much functionality, class components are much easier to work with, and also performance, class based components are more performant than function based ones if you are doing frequent updates. Things get even more complicated once you start to memoise things and need to add dependency arrays and stuff. I have a pretty big webapp, I've used class components for the "macro" pages, as in, the ones which handle the overall state of the whole app, then I used functional components to make all the rest. In short: use functional components if you don't re render them often and have simple state Use class components if the component is big or does many rerenders

  • @SonnySangha

    @SonnySangha

    2 жыл бұрын

    I disagree with your points, at a fundamental level functional components are lighter, more performant, and simpler to understand and implement than class based components. The above points you mentioned would only favour class based components if you aren’t applying them in a correct manner, this typically happens due to a lack of understanding hence defaulting to class based components. There is a reason why react defaults to functional components now and is deprecating class based lifecycle functions.

  • @specy_

    @specy_

    2 жыл бұрын

    @@SonnySangha my webapp on normal use has up to 50 rerenders a second, the speed of the class component comes from the fact that you don't need to "re run" and build all the functions that reside in the component itself but only the render method, while function components recreate everything at every render, you could use hooks like useCallback to improve this, but that means having to manage dependencies which is kind of annoying once you get to bigger components. I'm slowly moving away from class components and moving to functional components + mobx for state management. I have to point out that for the usual website or program, function components are fine for everything, Even larger ones, mine is more of a "special case"

  • @decathlonbo

    @decathlonbo

    Жыл бұрын

    ​@@SonnySangha There is no inherent performance benefit of Hooks over Class based components or vice versus. Both have the same performance. The React team has stated they were going to provide some features that FCs + Hooks would benefit from, but nothing has been updated in that regard. @Specy I don't think you can say that Class Components are more performant for frequent updates, and then provide ways we mitigate that in Functional Components and then say you just don't like having to manage dependencies and such. Imo, handling dependencies and doing the shallow comparison is much easier and cleaner than having to handle that in a Class lifecycle method. Long story short, FCs vs Classes really comes down to preference at this time. Neither is more performant than the other, and frequent re-renders wouldn't be a reason for why you'd choose one over the other. It's why we memoized and dependency arrays.

  • @binaprajapati7709
    @binaprajapati77092 жыл бұрын

    love from india dude 13 yr old here 😍

  • @jasonchen8566
    @jasonchen85662 жыл бұрын

    Sonny is the best!

  • @eliuddyn
    @eliuddyn2 жыл бұрын

    100k 🔥🔥🔥

  • @decathlonbo
    @decathlonbo Жыл бұрын

    I think one thing that always gets missed with these discussions is Custom Hooks. Most people who are coming from Class based components have trouble seeing the value of Functional Components. I think you did a good job explaining this, but the real value in my opinion is the ability to pull out ALL of the state logic and all of your side effects into their own custom hooks which can be consumed across all of your other components. Which allows your components to only focus on the UI and not have to worry about state at all. When I demonstrate this... that is usually when the lightbulb goes off for most people. Obviously this could be handled previously in class components with HOCs or some type of service based architecture & pure components... but with Hooks, it's so much easier to do it.

  • @thatsalot3577
    @thatsalot35772 жыл бұрын

    I know that people prefer to use functional components but as an ex-game developer class component look much simpler to me as they're a bit similar to the monobehaviour classes from unity. edit: After working for more than a year in react, I'd prefer functional components they have better features and are easier to work with.

  • @jsomhorst

    @jsomhorst

    Жыл бұрын

    I really don't understand why people are so fond about functional components. To me, as a Java developer, classes are a way to accomplish this. Also the use of 'const' as being a variable is just pure nonsense. A const is a constant and not something that should be changed... just my 2 cts ;)

  • @latelatecheung

    @latelatecheung

    Жыл бұрын

    why leave game industry? if I have ability to be a game programmer, I do not want go to develop web

  • @thatsalot3577

    @thatsalot3577

    Жыл бұрын

    @@latelatecheung because I felt like being trapped inside unity, I never knew what's it's like to write low level code or something that runs on the web, I still love game development but I was really burnt out as I had to make models, textures, UI, animations, all the sounds, shaders and script for every game object all by myself, it soon started to feel overwhelming and my games were laggy af, Yeah I know I can make 2d games much faster (and as an Indie I should) but I prefer 3d games, I still make a few projects in unity and dots in my part-time but you know, I have bills to pay and learning javascript is an easy way to do that. I'm a lot mature programmer now, I may make games in my free time just not as a full time gamedev.

  • @thatsalot3577

    @thatsalot3577

    Жыл бұрын

    @@latelatecheung and if you're thinking I should use Unreal engine or something else, then yeah I tried and it just doesn't run that good with my 8 gigs of RAM.

  • @latelatecheung

    @latelatecheung

    Жыл бұрын

    @@thatsalot3577 I know you feel bro. hope you get good pay in web industry

  • @meer-cha4985
    @meer-cha49852 жыл бұрын

    What an absolute legend

  • @shinigami2669
    @shinigami2669 Жыл бұрын

    I love to use functional components.

  • @testcert1767
    @testcert17672 жыл бұрын

    Useful

  • @aribiali3574
    @aribiali35742 жыл бұрын

    you are my guy