key differences between React class components and functional components

404code

404code

Posted on February 12, 2024

key differences between React class components and functional components

In the early days of React, class components were the go-to way of creating components. With the introduction of React Hooks, functional components gained more prominence.

React Class Components:

import React, { Component } from 'react';

class MyClassComponent extends Component {
  constructor(props) {
    super(props);

    // Initial state setup
    this.state = {
      data: [],
    };
  }

  // Lifecycle method: componentDidMount
  componentDidMount() {
    // Simulating an API call or any asynchronous operation
    setTimeout(() => {
      const mockData = ['Item 1', 'Item 2', 'Item 3'];

      // Updating state after the asynchronous operation
      this.setState({
        data: mockData,
      });
    }, 1000);
  }

  render() {
    return (
      <div>
        <h1>MyClassComponent</h1>
        {/* Rendering data from state */}
        <ul>
          {this.state.data.map((item, index) => (
            <li key={index}>{item}</li>
          ))}
        </ul>
      </div>
    );
  }
}

export default MyClassComponent;


Enter fullscreen mode Exit fullscreen mode

State Management:

  • Class components can have local state managed through this.state.

  • You can use lifecycle methods like componentDidMount and componentDidUpdate

Usage of 'this':

  • In class components, you often use this to refer to the instance of the class.

Legacy Code:

  • Older React codebases might have class components.

React Functional Components with Hooks:

import React, { useState, useEffect } from 'react';

const MyFunctionalComponent = () => {
  const [state, setState] = useState(initialState);

  useEffect(() => {
    // Effect logic (similar to componentDidMount and componentDidUpdate)
  }, [dependencies]);

  return (
    <div>

    </div>
  );
}
Enter fullscreen mode Exit fullscreen mode

State Management:

  • Hooks like useState allow functional components to manage state.

  • useEffect replaces lifecycle methods for side effects.

Usage of 'this':

  • Functional components don't use this.

Conciseness:

  • Functional components with hooks are generally more concise and readable.

When to Use Each:

Class Components:

  • If you're working with an older codebase or integrating with libraries that use class components.
  • Need to manage complex state and lifecycle methods.
  • Functional Components with Hooks:

  • For new projects, as it's the modern and recommended approach.

  • When you want cleaner and more readable code.

  • When state and lifecycle methods are relatively simple.

In modern React development, functional components with hooks are widely favored for their simplicity and maintainability. However, understanding class components is still valuable, especially when dealing with existing code. As you continue your full-stack web development journey, mastering both approaches will be beneficial.

💖 💪 🙅 🚩
404code
404code

Posted on February 12, 2024

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

Sign up to receive the latest update from our blog.

Related