What is class component in React?

Experience Level: Junior
Tags: React

Answer

In React, there are two types of components: functional and class components. A class component is a JavaScript class that extends the React.Component class. It is used to define a component with more complex logic and state management. Class components have access to the component lifecycle methods, such as componentDidMount and componentDidUpdate, which allow developers to manage the state of the component and perform actions when the component mounts or updates. Here is an example of a class component in React:


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

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

  componentDidUpdate() {
    console.log('Component updated');
  }

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

In this example, MyComponent is a class component that manages a count state and renders a button that increments the count when clicked. The componentDidMount and componentDidUpdate methods are used to log messages to the console when the component mounts and updates, respectively.

Class components are useful when you need to manage complex state and logic in your component. They also provide access to lifecycle methods that can be used to perform actions when the component mounts or updates. However, class components can be more verbose and harder to read than functional components, especially when dealing with complex state management. Here is an example of a functional component that achieves the same functionality as the previous class component:


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

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

  useEffect(() => {
    console.log('Component updated');
  });

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

In this example, MyComponent is a functional component that uses the useState and useEffect hooks to manage state and perform actions when the component mounts or updates. The code is more concise and easier to read than the previous class component.

In summary, a class component in React is a JavaScript class that extends the React.Component class. It is used to define a component with more complex logic and state management. Class components have access to the component lifecycle methods, such as componentDidMount and componentDidUpdate, which allow developers to manage the state of the component and perform actions when the component mounts or updates. However, functional components with hooks can achieve the same functionality with less code and are often easier to read and maintain.

React for beginners
React for beginners

Are you learning React ? Try our test we designed to help you progress faster.

Test yourself