Table of contents
1.
Introduction
2.
Functional Components
2.1.
Counter Using Functional Components
3.
Class Components
3.1.
Counter Using Class Components
4.
Functional Components vs Class Components
5.
Frequently Asked Questions
5.1.
Can functional components have state?
5.2.
When should I use class components over functional components?
5.3.
Are functional components better than class components?
6.
Conclusion
Last Updated: Aug 13, 2025
Easy

Class Component vs Functional Component

Author Gaurav Gandhi
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

When building web applications, developers often need to decide between using class components or functional components. This choice is crucial as it influences the code's structure, performance, and ease of maintenance. 

Class Component vs Functional Component

This article will discuss both types of components, explaining their syntax with proper examples, and the differences. This topic is one of the crucial aspect which eventually going to help us in making the code structure.

Functional Components

Functional components in React are simple JavaScript functions. These functions accept props as an argument and return React elements that describe what should appear on the screen. They do not use the this keyword, making them generally easier to manage and understand, especially for those new to React development.

One of the main advantages of using functional components is their simplicity. Without the complexity of managing this or component state (prior to the introduction of hooks), they are straightforward to test and integrate. Additionally, since React 16.8, functional components can fully utilize React hooks to manage state and lifecycle events, making them just as powerful as class components but with cleaner and more readable code.

Here is a basic syntax example:

function Welcome(props) {
  return <h1>Hello, {props.name}</h1>;
}


This example defines a functional component named Welcome that takes props and renders a heading with a greeting message. This type of component is useful for displaying simple pieces of the UI without needing to manage a state or lifecycle methods.

Counter Using Functional Components

Now, let’s create a simple counter. This example will show how you can manage state in functional components using hooks, specifically the useState hook.

Here is the complete code for a counter:

import React, { useState } from 'react';
function Counter() {
  const [count, setCount] = useState(0);
  const incrementCount = () => {
    setCount(count + 1);
  };
  const decrementCount = () => {
    setCount(count - 1);
  };
  return (
    <div>
      <h1>Current Count: {count}</h1>
      <button onClick={incrementCount}>Increase</button>
      <button onClick={decrementCount}>Decrease</button>
    </div>
  );
}


In this example, useState is used to create count state variable. The setCount function allows us to update the count when the buttons are clicked. The UI consists of a heading displaying the current count and two buttons to increase or decrease the count. This counter demonstrates the simplicity and power of functional components for state management in modern React applications.

Class Components

Class components in React are more complex than functional components because they allow more features such as local state management and lifecycle methods. A class component requires extending React.Component and must include at least the render() method, which returns React elements.

Here is a basic syntax example for a class component:

import React, { Component } from 'react';
class Welcome extends Component {
  render() {
    return <h1>Hello, {this.props.name}</h1>;
  }
}


In this example, Welcome is a class component that takes props and renders a greeting message. Unlike functional components, class components make use of this to access props and state. This makes them suitable for more complex scenarios where state or lifecycle handling is necessary.

Class components can also manage local state and react to lifecycle events, such as when a component mounts or updates. This capability is essential for handling side effects, managing subscriptions, and more, which can be crucial for more complex applications.

Counter Using Class Components

Now let's look at how a counter would be implemented using class components in React. This example will help illustrate the management of state and lifecycle methods that are available in class components, providing a more traditional approach compared to functional components.

Here is the complete code for a counter using a class component:

import React, { Component } from 'react';
class Counter extends Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }
 incrementCount = () => {
    this.setState({ count: this.state.count + 1 });
  };
 decrementCount = () => {
    this.setState({ count: this.state.count - 1 });
  };
 render() {
    return (
      <div>
        <h1>Current Count: {this.state.count}</h1>
        <button onClick={this.incrementCount}>Increase</button>
        <button onClick={this.decrementCount}>Decrease</button>
      </div>
    );
  }
}


In this example, the Counter class component maintains its state that includes a count variable. It uses the setState method to update the state whenever the increase or decrease buttons are clicked. The render method then returns the UI part, which shows the current count and buttons to modify it. This setup demonstrates how class components can handle more complex state changes and interactions within a component.

Functional Components vs Class Components

Feature Functional Components Class Components
Complexity Less complex, more straightforward to use. More complex, provide more control and features.
State management Use hooks like useState to manage state. Manage state through this.state and setState.
Lifecycle methods  Use hooks like useEffect for lifecycle events. Direct access to lifecycle methods like componentDidMount.
Syntax Simpler, concise syntax using functions. Requires extending React.Component and writing more boilerplate code.
Performance Improved with hooks, comparable to class components. Initially slower than functional components, but optimized in recent React versions.
Suitability Best for simple components or those that require hooks for state/effects. Preferred for components that need direct access to lifecycle methods and more complex state logic.

Frequently Asked Questions

Can functional components have state?

Yes, functional components can manage state using the useState hook, allowing them to maintain internal state without needing to be class components.

When should I use class components over functional components?

Use class components when you need more control over the component lifecycle, or when dealing with complex state logic that might benefit from being in a class structure.

Are functional components better than class components?

"Better" depends on the context. Functional components are generally simpler and more concise, making them ideal for most cases, especially with the addition of hooks. However, class components might be necessary for more complex scenarios.

Conclusion

In this article, we have learned the distinct differences between functional and class components in React. We covered the simplicity and efficiency of functional components, their use of hooks for managing state and lifecycle events, and compared these features against the more traditional class components, which offer more control through direct access to lifecycle methods and state management. 

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure andAlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry.

Live masterclass