Subscribe
Converting React Class Components to Functional Components
5 mins read

By: vishwesh

Converting React Class Components to Functional Components

Introduction

React components are the building blocks of React applications. There are two types of React components: class components and functional components. Class components are the older way of creating components in React and are defined using ES6 classes. They have a lot of boilerplate code, making them harder to read and maintain. In contrast, functional components are simpler and more lightweight. They are defined using functions and can be easily converted from class components. In this article, we'll go over the steps required to convert React class components to functional components.

Step 1: Remove the Constructor and Extends

The first step in converting a class component to a functional component is to remove the constructor and extends keywords. These are only used in class components and are not needed in functional components.

// Class Component
import React, { Component } from 'react';

class App extends Component {
  constructor(props) {
    super(props);
    this.state = {
      message: 'Hello World',
    };
  }

  render() {
    return (
      <div>
        <h1>{this.state.message}</h1>
      </div>
    );
  }
}

// Functional Component
import React from 'react';

function App() {
  return (
    <div>
      <h1>Hello World</h1>
    </div>
  );
}

In the above example, we removed the constructor and extends keywords from the class component and converted it to a functional component.

Step 2: Add State Variables with useState Hook

In class components, state is declared in the constructor and can be updated using this.setState. In functional components, state is declared using the useState hook. The useState hook is a built-in React hook that allows us to add state to functional components. It takes an initial state value as an argument and returns an array containing the current state and a function to update the state.

// Class Component
import React, { Component } from 'react';

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

  handleClick() {
    this.setState((prevState) => ({
      count: prevState.count + 1,
    }));
  }

  render() {
    return (
      <div>
        <h1>Counter</h1>
        <p>{`Count: ${this.state.count}`}</p>
        <button onClick={this.handleClick}>Increment Count</button>
      </div>
    );
  }
}

// Functional Component
import React, { useState } from 'react';

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

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

  return (
    <div>
      <h1>Counter</h1>
      <p>{`Count: ${count}`}</p>
      <button onClick={handleClick}>Increment Count</button>
    </div>
  );
}

In this example, we added a state variable to our component using the useState hook. We set the initial value of the count variable to 0 and created an event handler called handleClick to update the count state variable using the setCount function.

Step 3: Update Event Handlers

Event handlers in class components use the this keyword to refer to the component instance. In functional components, event handlers use state variables declared using the useState hook.

// Class Component
import React, { Component } from 'react';

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

  handleClick() {
    this.setState((prevState) => ({
      count: prevState.count + 1,
    }));
  }

  render() {
    return (
      <div>
        <h1>Counter</h1>
        <p>{`Count: ${this.state.count}`}</p>
        <button onClick={this.handleClick}>Increment Count</button>
      </div>
    );
  }
}

// Functional Component
import React, { useState } from 'react';

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

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

  return (
    <div>
      <h1>Counter</h1>
      <p>{`Count: ${count}`}</p>
      <button onClick={handleClick}>Increment Count</button>
    </div>
  );
}

In the example above, we converted the handleClick event handler to use the setCount function to update the count state variable.

Step 4: Convert Lifecycle Methods

Class components have a number of lifecycle methods that are called at different stages of the component's life, such as componentDidMount, componentDidUpdate, and componentWillUnmount. In functional components, we can use the useEffect hook to mimic the behavior of these lifecycle methods.

// Class Component
import React, { Component } from 'react';

class LifecycleDemo extends Component {
  constructor(props) {
    super(props);
    this.state = {
      message: '',
    };
  }

  componentDidMount() {
    this.setState({ message: 'Component mounted.' });
  }

  componentDidUpdate(prevProps, prevState) {
    if (prevState.message !== this.state.message) {
      console.log('State updated.');
    }
  }

  componentWillUnmount() {
    console.log('Component unmounted.');
  }

  render() {
    return <h1>{this.state.message}</h1>;
  }
}

// Functional Component
import React, { useState, useEffect } from 'react';

function LifecycleDemo() {
  const [message, setMessage] = useState('');

  useEffect(() => {
    setMessage('Component mounted.');
    return () => console.log('Component unmounted.');
  }, []);

  useEffect(() => {
    console.log('State updated.');
  }, [message]);

  return <h1>{message}</h1>;
}

In this example, we used the useEffect hook to mimic the behavior of the componentDidMount, componentDidUpdate, and componentWillUnmount lifecycle methods. We set the initial value of the message state variable to an empty string and then used the useEffect hook to set the message to 'Component mounted' when the component mounts. We then used another useEffect hook to log 'State updated' whenever the message state variable changes. Finally, we used the useEffect hook to log 'Component unmounted' when the component is unmounted.

Conclusion

Here is a complete example of converting a class component to a functional component:

// Class Component
import React, { Component } from 'react';

class MyComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {
      message: '',
      count: 0,
    };
    this.handleClick = this.handleClick.bind(this);
  }

  componentDidMount() {
    console.log('Component mounted.');
  }

  componentDidUpdate(prevProps, prevState) {
    if (prevState.count !== this.state.count) {
      console.log('Count updated.');
    }
  }

  componentWillUnmount() {
    console.log('Component unmounted.');
  }

  handleClick() {
    this.setState((prevState) => ({
      count: prevState.count + 1,
      message: `Count is ${prevState.count + 1}`,
    }));
  }

  render() {
    return (
      <div>
        <h1>{this.state.message}</h1>
        <button onClick={this.handleClick}>Increment Count</button>
      </div>
    );
  }
}

// Functional Component
import React, { useState, useEffect } from 'react';

function MyComponent() {
  const [message, setMessage] = useState('');
  const [count, setCount] = useState(0);

  useEffect(() => {
    console.log('Component mounted.');
    return () => console.log('Component unmounted.');
  }, []);

  useEffect(() => {
    console.log('Count updated.');
    setMessage(`Count is ${count}`);
  }, [count]);

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

  return (
    <div>
      <h1>{message}</h1>
      <button onClick={handleClick}>Increment Count</button>
    </div>
  );
}

In this example, we converted the MyComponent class component to a functional component. We removed the constructor and extends keywords, and replaced the this.state and this.handleClick references with state variables declared using the useState hook and event handlers that use those state variables.

We also used the useEffect hook to mimic the behavior of the componentDidMount, componentDidUpdate, and componentWillUnmount lifecycle methods.

Overall, converting class components to functional components is a simple process that can help make your code more concise and easier to understand. By using hooks like useState and useEffect, you can achieve the same functionality as class components in a more modern and streamlined way.

Recent posts

Don't miss the latest trends

    Popular Posts

    Popular Categories