DEV Community

Cover image for Converting React Class Components to Functional Components: A Checklist and Example
Jesse R Weigel
Jesse R Weigel

Posted on

Converting React Class Components to Functional Components: A Checklist and Example

When converting a React class component to a functional component, you can follow this checklist:

  1. Create a Functional Component: Start by creating a new functional component using the function keyword or an arrow function.

  2. Move render Method Content: Move the content of the render method from the class component into the body of the functional component. This content will be directly returned by the functional component.

  3. Replace this.props with props: In the functional component, replace all instances of this.props with props. If you're using destructuring, you can extract the required props directly from the function's arguments.

  4. Handle State with useState: If the class component uses state, replace the this.state object with the useState hook. For each state variable, create a separate useState call and update the corresponding references in the component.

  5. Handle Lifecycle Methods with Hooks: Replace lifecycle methods such as componentDidMount, componentDidUpdate, and componentWillUnmount with the useEffect hook. You may need to use multiple useEffect hooks with different dependencies to replicate the behavior of different lifecycle methods.

  6. Replace Class Methods with Functions: Convert class methods into regular functions or arrow functions within the functional component. Update the method calls accordingly.

  7. Remove this: Remove all instances of the this keyword, as it is not used in functional components.

  8. Handle Context with useContext: If the class component uses context, replace the Context.Consumer pattern with the useContext hook.

  9. Update Event Handlers: Update event handlers to reference the new functions created in step 6. Remove any bindings that were previously required for class methods.

  10. Test the Component: Thoroughly test the converted functional component to ensure that it behaves as expected. Verify that state updates, event handlers, and side effects work correctly.

Here's an example of converting a simple class component to a functional component:

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

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

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

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

export default Counter;

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

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

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

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

export default Counter;
Enter fullscreen mode Exit fullscreen mode

In this example, the class component Counter is converted to a functional component. The state and increment method are replaced with the useState hook and a regular function, respectively. The render method content is directly returned by the functional component.

Top comments (0)