Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

React - Are class components ever used?

Tags:

reactjs

I'm going through some courses that were made before react hooks were a thing, now I'm wondering if there is any reason at all to use class-based components over functional components now that hooks exist?

I can't seem to come up with a reason or scenario where a class-based component would be preferable, but maybe I'm just not aware of certain corner-cases where a class-based component would be the go-to choice.

Appreciate any feedback regarding this.

Thanks in advance!

like image 719
xcynic Avatar asked Jul 30 '26 05:07

xcynic


2 Answers

Class components are still used, but there is no particular reason to continue to do so. Tthe Facebook team recommends that all new React code is hook-based functional components, and not class-based. You can mix class components and hook-based components, so there is no reason to rewrite your class components.

You can read more at https://reactjs.org/docs/hooks-faq.html

like image 181
Stephan Bakkelund Valois Avatar answered Aug 01 '26 19:08

Stephan Bakkelund Valois


The only good reason that I can think of for using class components these days would be to create error boundaries as:

Only class components can be error boundaries.

From https://reactjs.org/docs/error-boundaries.html

like image 22
JamieJag Avatar answered Aug 01 '26 19:08

JamieJag



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!