DEV Community

Cover image for Functional Components vs Class components in React
Satya Samaikya Venna
Satya Samaikya Venna

Posted on

Functional Components vs Class components in React

Introduction

React offers two main ways to define components: Functional and Class-based. Over time, functional components have become the standard, especially with the introduction of React Hooks in version 16.8. In this post, weโ€™ll break down both types, explore their differences, and help you understand when and why to use one over the other.

๐Ÿ”น What Are React Components?

Components are the building blocks of any React application. Each component represents a piece of UI. There are two primary types:

  • Functional Components

  • Class Components

๐Ÿ”น Functional Components

โœ… Definition
A functional component is a plain JavaScript function that returns JSX. Before hooks, functional components were limited and couldn't manage state or side effects โ€” now they can do almost everything class components can.

import React from 'react';

function App(){
    return(
       <div>
         <h1>Hello World</h1>
       </div>
 )
}
export default App;
Enter fullscreen mode Exit fullscreen mode

โœ… Advantages

  • Concise and easier to read

  • No need for this keyword

  • Full feature support via Hooks

  • Preferred in modern React development

๐Ÿ”น Class Components

โœ… Definition
Class components are ES6 classes that extend from React.Component. They have a render() method and access to lifecycle methods.

import React, { Component } from 'react';

class App extends Component{
      render(){
         return(
            <div>
               <h1>Hello World</h1>
            </div>
      )
}
export default App;

Enter fullscreen mode Exit fullscreen mode

โ— Drawbacks

  • More boilerplate

  • Use of this can be confusing

  • Lifecycle methods are less intuitive than hooks

๐Ÿ”น Comparison Table

Feature Functional Component Class Component
Syntax Function/Arrow Function ES6 Class
State useState Hook this.state
Side Effects useEffect Hook Lifecycle methods
this keyword โŒ Not required โœ… Required
Boilerplate Minimal Verbose
Code Reusability Custom Hooks HOCs, Render Props
Performance Slightly better (modern) Slightly heavier

๐Ÿ”น When to Use Functional Components

  • In modern React projects (React 16.8+)

  • When you want concise, maintainable code

  • When using hooks for shared logic

๐Ÿ”น When Class Components Are Still Useful

  • Maintaining legacy codebases

  • Understanding React's history and lifecycle

  • Some advanced patterns still use classes (e.g., Error Boundaries)

๐Ÿ”น Conclusion

While both functional and class components are valid in React, the trend has shifted toward using functional components thanks to the power of hooks. They make code easier to understand, test, and maintain.

If youโ€™re starting a new project or migrating old code, go with functional components โ€” they're the future of React.

โœ๏ธ Final Thoughts

React continues to evolve, and staying updated is essential. Embracing functional components and hooks will help you write cleaner, more efficient React apps.

Top comments (0)