Skip to main content

Class Components

In the early days of React (before Hooks were introduced in React 16.8), class-based components were the primary way to define components with state and lifecycle methods. While functional components with Hooks are now often preferred for their simplicity and ease of use, class components still have their place in React development, especially for complex components or those requiring access to specific lifecycle methods.

Creating Class Components

Class components are created by extending the built-in React.Component class. They define the component's behavior using methods, including a mandatory render method that specifies how the UI is rendered.

Here's a basic example:

import React from 'react';

class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

export default Greeting;

In this example:

  • We import React to create React components.
  • We define a class named Greeting that extends React.Component.
  • Inside the class, we define a render method that returns an h1 element displaying a greeting message. The message includes the name prop passed to the component.

Props

Components receive data from parent components through props (properties). Props are read-only and cannot be modified within the child component. They are passed as an object argument to the component's constructor (although typically accessed directly within the render method).

Here's how you can use props in a class component:

class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}

In this example, the Greeting component receives a name prop, which is used within the JSX to personalize the greeting message.

State

State is a special type of data managed by a class component. Unlike props, state can be changed over time, triggering a re-render of the component to reflect the updated state. You can use the useState hook (introduced in later React versions) or the class-based approach as shown here:

class Counter extends React.Component {
constructor(props) {
super(props); // Call the parent constructor
this.state = { count: 0 }; // Initialize state with initial count
}

handleClick = () => {
this.setState({ count: this.state.count + 1 }); // Update state
};

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

In this example:

  • The constructor initializes the component's state with an object containing a count property set to 0.
  • The handleClick method is defined to handle button clicks. It uses this.setState to update the count state by 1.
  • The render method displays the current count value and a button that triggers the handleClick method when clicked.

Lifecycle Methods

Class components provide lifecycle methods that allow you to perform actions at specific points in the component's lifecycle. Some common lifecycle methods include:

  • componentDidMount: This method is invoked immediately after a component is mounted (inserted into the DOM). It's a good place to perform side effects like fetching data from an API.
  • componentDidUpdate: This method is invoked immediately after updating a component (when its state or props change). It's useful for performing actions based on state changes.
  • componentWillUnmount: This method is invoked immediately before a component is unmounted (removed from the DOM). It's often used for cleaning up resources or subscriptions.

These are just a few key aspects of class-based components in React. While Hooks often provide a more concise and functional approach, understanding class components is still valuable for projects using older React versions or for scenarios requiring specific lifecycle methods.