Functional and Class Component in React.js: Exploring React's Evolution

cybermaxi7

Cybermaxi7

Posted on August 31, 2023

Functional and Class Component in React.js: Exploring React's Evolution

In the world of React development, there's an ongoing debate about the choice between Class Components and Functional Components. Each has its own advantages and use cases. In this article, we'll delve into the key differences between these two approaches and shed light on which one might be more suitable for your projects.

Evolution of Components
Functional Component: A modern approach that gained prominence with the introduction of hooks in React 16.8 (released in 2019). It's important to note that function components existed even before hooks (pre-React 16.8), but their capabilities were limited without the ability to manage state.

Class Component: The older approach, dating back to React 0.13 in 2015. Class components have been a staple in React development for a long time and are still used in various codebases.

Note: React's function components existed before hooks, but they lacked certain features like local state management.

Creating Components
In React, creating components is the foundation of building UIs. Let's compare how Class Components and Functional Components are created:

  • Functional Component: These are created using standard JavaScript functions. This can be a function declaration or an arrow function.
const FunctionalComponent = (props) => {
  // Component logic here
};
Enter fullscreen mode Exit fullscreen mode
  • Class Component: Class components are built using ES6 classes that extend the React.Component class.
class ClassComponent extends React.Component {
  // Component logic here
}

Enter fullscreen mode Exit fullscreen mode

Handling Props and State
Props and state are essential aspects of React components. Let's see how they are managed in both Class and Functional Components:

  • Class Component: In class components, props are accessed using this.props.X, and local state is managed using this.setState.

  • Functional Component: Functional components simplify this process. Props can be accessed using destructuring, like { X } = props. Local state is managed using the useState hook.

Managing Side Effects and Lifecycle
One of the most significant differences between Class and Functional Components is how they handle side effects and component lifecycle.

  • Class Component: React's class components rely on lifecycle methods such as componentDidMount and componentWillUnmount for managing side effects and synchronization.

  • Functional Component: In functional components, the focus is on synchronization rather than a traditional lifecycle. This is achieved using the useEffect hook, which provides a more intuitive and streamlined way to manage side effects.

Event Handling and JSX Return
Functional Component:
In a functional component, event handlers can be defined as regular functions within the component's body. JSX is returned directly from the function.

import React from 'react';

const FunctionalComponent = () => {
  const handleClick = () => {
    console.log('Button clicked in functional component');
  };

  return (
    <div>
      <button onClick={handleClick}>Click me (Functional)</button>
    </div>
  );
};

export default FunctionalComponent;

Enter fullscreen mode Exit fullscreen mode

Class Component:
In a class component, you need to define separate methods for each event handler. JSX is returned from the render method.

import React from 'react';

class ClassComponent extends React.Component {
  handleClick() {
    console.log('Button clicked in class component');
  }

  render() {
    return (
      <div>
        <button onClick={this.handleClick}>Click me (Class)</button>
      </div>
    );
  }
}

export default ClassComponent;

Enter fullscreen mode Exit fullscreen mode

Managing Side Effects and Lifecycle
Functional Component:
In functional components, you can use the useEffect hook to handle side effects and synchronization, similar to how you would use lifecycle methods in class components.

import React, { useEffect } from 'react';

const FunctionalComponent = () => {
  useEffect(() => {
    console.log('Functional component mounted');
    return () => {
      console.log('Functional component unmounted');
    };
  }, []);

  return <div>Functional Component</div>;
};

export default FunctionalComponent;

Enter fullscreen mode Exit fullscreen mode

Class Component:
In class components, lifecycle methods like componentDidMount and componentWillUnmount are used to manage side effects and synchronization.

import React from 'react';

class ClassComponent extends React.Component {
  componentDidMount() {
    console.log('Class component mounted');
  }

  componentWillUnmount() {
    console.log('Class component unmounted');
  }

  render() {
    return <div>Class Component</div>;
  }
}

export default ClassComponent;

Enter fullscreen mode Exit fullscreen mode

Advantages of Functional Components
Functional components offer several advantages over class components:

  • Ease of Development: Functional components require less boilerplate code, making them quicker to develop.

  • Cleaner Code: The useEffect hook consolidates all lifecycle-related code, resulting in cleaner and more organized code.

  • Custom Hooks: Functional components, in conjunction with custom hooks, facilitate the sharing of strategic logic across components.

  • No More this: Functional components eliminate the need for using the this keyword.

The Class Component's Edge
While functional components have gained dominance, class components still hold an advantage in terms of beginner-friendliness due to their more straightforward lifecycle methods, such as componentDidMount and componentWillUnmount.

Thank you for joining me on this exploration of Class Components and Functional Components in React. We hope this article has shed light on the differences between these two approaches and helped you understand their respective strengths. If you found this article insightful, please consider liking, commenting, and sharing to spread the knowledge!

Check me out on
LinkedIn
Twitter

Happy coding!

💖 💪 🙅 🚩
cybermaxi7
Cybermaxi7

Posted on August 31, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

React Basics~unit test/custom hook
webdev React Basics~unit test/custom hook

October 27, 2024

React Basics~unit test/async test
webdev React Basics~unit test/async test

October 24, 2024

React Basics~unit test/user event
webdev React Basics~unit test/user event

October 21, 2024

React Basics~unit test/ui
webdev React Basics~unit test/ui

October 20, 2024