DEV Community

React Hooks versus Classes - why choose one over another?

Ngan Kim Khong on August 11, 2020

Recently an interviewer asked me about choosing between a functional component and class component. Why do I write functional component instead of ...
Collapse
 
kaizenlabs profile image
Kaizen Technology

Hi Ngan, you would use a class-based component when you need the component to manage its own local state, or if you need the component to utilize lifecycle methods. Technically yes, hooks do allow you maintain local state in a functional component, functional components are declarative style while classes are OOP style.

Most enterprise-based React apps are built with class-based components and they are particularly interested in refactoring their apps to be purely functional components, I still think class-based will be the dominating paradigm going forward because it helps distinguish between between which components are stateful components (class-based) and which are just passed state as a container (functional).