What are React JS Class Components?

What are React JS Class Components?

If you are fond of web development or are starting your journey in it then you must have heard about React JS. Well also must know that everything about React JS revolves around components. One can define a component either by using classes or functions. In this article, we will understand what React JS class is, how to create it, and its benefit in development.  

What are React JS Components?

A component in React is an independent and reusable block of code that has unique functionality. When such components are combined together it helps to make a dynamic and user-friendly UI for the web application. In other words, we can say that components are lego blocks for the webpage or UI that we are implementing.

Without the use of components, thousands lines of code would be needed to put together in a single file. This increases the maintenance and complexity of the code as the project grows. To avoid all this, components are used widely. There are two main types of components, functional or stateless and class or stateful. We will be majorly focusing on the later half, that is React JS Class components in detail.

What is a React JS Class?

React JS Class components are ES6 classes and are more complex to build as compared to functional components. Each React JS class includes constructors, life cycle methods, render function and also state or data management. To make use of classes in React JS, the class has to extend Component. Then only React JS understands that this particular component is a class and then will render or return the required react element.

Main features of a React JS Class

Now that we are aware of what classes are in React, let us discuss the main features all React JS class components have. So a React class component,

  1. Is an ES6 class
  2. Will only become a component once it extends the Component of React JS
  3. Can accept props if needed in the constructor
  4. Maintains its own data by the help of state
  5. There should always be a render function which will either return a react element or null.
  6. Then classes manage the state of the component
  7. One can add life cycle methods to the component though classes

How to write a React JS class?

If you are familiar with classes, then programming using React JS class components will be a piece of cake for you. Below is the standard syntax you will need to use in order to define a class component:

class MyClassComponent extends React.Component{
   <elementOrNull ….. />

The need for React JS Class component

Earlier while programming in React, the developers had a choice when creating a component. One could make simple functions and return or inherit the React.Component class from the library. The first method created functional components while the latter were known as class components. 

Functional components were simple and easy to use, while React JS class components were complex to program but would provide a lot of functionality. You were provided with many lifecycle methods which made component mounting and unmounting easy. Also accessing and updating the state of the component was very easy.  

Current scenario of React JS Class

But then in 2018, the React team released React Hooks. They provided the same ability to functional components which lifecycle methods provided to class components. Many developers are switching to React hooks. There is no confusion caused by the keyword “this” and the number of lines for a particular task is less in comparison to class components. Many companies like Facebook are majorly using React hooks for these reasons.

There are downsides to using React hooks as well! For instance, relearning new syntax is tedious and time-consuming. If you are already well versed with using classes then using functional components creates a lot of misunderstanding. Also there is not a large gap between the performance level by using functional or class components. But since functional components do not have access to shouldComponentUpdate and PureComponent, there could be a little difficulty for performance optimization.

Is React JS class still used?

Now you must be wondering which is better, functional components or class components? To be honest they both are amazing and one should use a component which they can understand and can code comfortably in. There are certain scenarios where you do not have a choice and you have to make use of React JS class components. These cases are given below:

  • getSnapshotBeforeUpdate()
  • componentDidCatch()
  • getDerivedStateFromError()

There are no equivalent react hooks for these methods till now. But in the future, the React development team has plans to create such functions for hooks as well. Although, till that time comes, React JS classes are very important and there is definitely a need to use them in today’s world.


We discussed what a React JS Class is and how to write a basic class component. We also explained how classes are important and how the React developer team has released React hooks to give class components a competition. If you are confused about different concepts of React JS or other technologies then do not forget to check out Codedamn! If you have any queries or suggestions, do let us know in the comments!

Sharing is caring

Did you like what Agam singh, Aman Ahmed Siddiqui, Aman Chopra, Aman, Amol Shelke, Anas Khan, Anirudh Panda, Ankur Balwada, Anshul Soni, Arif Shaikh, wrote? Thank them for their work by sharing it on social media.


No comments so far