Home > Enterprise >  React Functional Component and Class Component
React Functional Component and Class Component

Time:03-31

I wonder if there is any specific uses for each type of component.

Im just learning react and wonder if I can create a system entirely built with functional components only.

I know functional component support hooks and Class Component support lifecycle methods.

does hooks is a counterpart of methods in lifecycle?

Or hooks have different usage? and so are Lifecycle methods?

When to use Class, at the same time when to use Functional?

.....................................

CodePudding user response:

In the last 2 years, I completed 4 react projects only by using hooks. And according to my experience, it is enough to use only use hooks throughout your project. Lifecycles can be handled easily with the useEffect function. It is easy really. And you can always create a class-based component in your project when it is needed. If you choose one, it doesn't mean you have to implement the same for all of your components.

In my projects, there were a couple of places I used class-based components such as web-rtc, socket connection required components etc.

CodePudding user response:

Earlier functional components were stateless which means that you needed to use class components to manage the state, but now functional components supports hooks which makes it very easy.and yes you can create whole system with only functional components I'm working in a company which has a large system and we are refactoring the old code of class components to functional components.

  • Related