DEV Community

Yu Hamada
Yu Hamada

Posted on

1 1

Understanding Function Components vs Class Components in 100 seconds.

Table of Contents

  1. At the beginning
  2. What is Functional Components?
  3. What is Class Components?
  4. Benefit of using Functional Components
  5. Conclusion

At the beginning

According to React official document, using functional components is highly recommended rather than class components right now. React 16.8 introduced "React Hooks" for Functional Components and brought which were previously reserved for Class Components to Functional Components.

Functional Components

import React from 'react'

export default function FunctionalComponent() {
  return <div>Hello World!</div>
}
Enter fullscreen mode Exit fullscreen mode

Class Components

import React, { Component } from 'react'

export default class ClassComponent extends Component {
  render() {
    return <div>Hello World!</div>
  }
}
Enter fullscreen mode Exit fullscreen mode

What is Functional Components?

Functional Components are JavaScript functions that take in props as an argument and return JSX(user interfaces). They are easier to write and understand, especially for beginners.
In React 16.8, hooks allow functional components to use state and other React features without writing a class.

Here is an example of counter component using Functional Components.

Counter Component using Functional Components

import React, { useState } from 'react';

function Counter() {
  const [count, setCount] = useState(0);

  const increment = () => {
    setCount(count + 1);
  };

  return (
    <div>
      <p>Count: {count}</p>
      <button onClick={increment}>Increment</button>
    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

What is Class Components?

In comparison to functional components, Class Components are more complex. Class Components have been the traditional way of creating components, which are defined by using class of JavaScript. In React, They return JSX(user interfaces) by render(). And also, use state and lifecycle instead of React Hooks in Functional Components.

Here is an example of counter component using Class Components.

Counter Component using Class Components

class Counter extends React.Component {
  constructor(props) {
    super(props);
    this.state = { count: 0 };
  }

  increment = () => {
    this.setState({ count: this.state.count + 1 });
  }

  render() {
    return (
      <div>
        <p>Count: {this.state.count}</p>
        <button onClick={this.increment}>Increment</button>
      </div>
    );
  }
}
Enter fullscreen mode Exit fullscreen mode

Benefit of using Functional Components

There are 4 benefits of Functional Components.

  1. Simple and Readable
  2. Improving the performance
  3. Reusable components
  4. React Hooks

Simple and Readable

Functional components are fundamentally simpler and more readable. This simplicity makes it easier for developers to understand the code, leading to faster development and easier maintenance.

Improving the performance

Functional components can lead to performance improvements in our applications by avoiding the overhead associated with class components such as creating instances.

Reusable components

Function components promote reusability. They are easier to extract into smaller, reusable components.
This reusability is essential for building scalable applications.

React Hooks

React Hooks, introduced in React 16.8, have revolutionized the way developers handle state management in function components. With hooks, you can manage component state and lifecycle events without the need for class-based components.

Conclusion

Now, Most of web applications are created by using Functional Components.
I would say we don't need to learn Class Components too much. Especially for beginners, there are lots of what we should learn in Functional Components including other frameworks based on this concept. So, It's better focus on learning and using Functional Components.

SurveyJS custom survey software

JavaScript UI Libraries for Surveys and Forms

SurveyJS lets you build a JSON-based form management system that integrates with any backend, giving you full control over your data and no user limits. Includes support for custom question types, skip logic, integrated CCS editor, PDF export, real-time analytics & more.

Learn more

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay