Class components are built using ES6 classes and must extend React.Component to inherit React’s functionality.

They have a render method that returns JSX and can maintain their own state directly within the class without hooks.

Class components also support lifecycle methods, such as componentDidMount and componentDidUpdate, which manage component behaviors at different stages.

class Greeting extends React.Component {
  constructor(props) {
    super(props);
    this.state = { message: "Hello" };
  }
 
  render() {
    return <h1>{this.state.message}, {this.props.name}!</h1>;
  }
}

In this example, Greeting is a class component that also displays a message, stored in this.stateand accessed via this.props.


react