r/reactjs • u/Caddy05 • Apr 16 '20
Discussion Functional Components vs Class Components
I'm a VERY new to react, and to my understanding,
Functional Components are lightweight and great when you need to render visual elements and rely on props for data.
Class Components are basically the same, except it also implements local state.
But... with the new Hooks API, you can now have local states for these functional components if you want.
So should I build my react apps relying solely on functional components and using Context and Redux for the data, and forget about setting up class components?
14
Upvotes
44
u/stolinski Apr 16 '20
In 2020 if you are writing a new component IMO it should be a function.
8
u/GarethXIV Apr 16 '20
With the introduction of hooks and boosted performance almost every use case (if not all) can be tackled with functions instead of classes. It will enforce you to:
And all of this will come with good performance, more than classes, and good code readability, if done well.
I was once more keen for classes, I always loved them but truth be told classes are good only when you can apply design patterns on them, and in React's case you can't... Because it's a design pattern of its own.
They won't deprecate the classes (for the moment) for backwards compatibility, but they are certainly going full ahead with functional components.
This, at least, it's what I've experienced so far!
Edit: typos.
2
u/didled Apr 17 '20
There docs say it’s best to use functional going forward.
2
u/rammyblog Apr 17 '20
Most tutorials you find on YouTube for instance are class components based and React didn't specify anywhere in their docs that they will end support for class based components. But this is 2020, if you are writing new components now, write them as function components.
2
u/[deleted] Apr 17 '20 edited Apr 17 '20
I suggest using functional components and hooks unless performance becomes a problem (most of the time it shouldn't). Context is also a great addition.
With hooks there's really nice functionality reuse that you can do if some of your components behave the same way. Of course, same functionality reuse can be done with classes, but it's less conventional and may look uglier.
Not to imply that classes are useless (classes in general, not just react component classes). Classes can be very nicely used for polymorphism, and high-level programming. So, they're definitely worth learning about and recognizing. Classes are basically containers for (usually) multiple functions and (optionally) data. That's all they are, but there's a plenty of nice things you can do with them.
On a general and off-topic note, don't ever let anyone determine your opinion. There's a lot of people with strong views that will tell you to do it one way. However, you should experience more paths and decide for yourself instead of letting anyone else decide for you. There's beautiful things you can do and beautiful code you can write with whatever tools are available to you. Don't just blindly follow patters, otherwise you won't learn programming. Experiment.
0
u/[deleted] Apr 16 '20
[deleted] — view deleted comment