A class component extends React.Component and must implement a render() method returning JSX; state lives on this.state, initialized in the constructor and updated exclusively through this.setState(), and side effects tied to the component's lifecycle are handled through dedicated methods like componentDidMount and componentDidUpdate rather than a single unified hook. Class components require careful attention to this binding, since a method like an event handler loses its automatic this context unless explicitly bound in the constructor or written as an arrow function class property — a common, easy-to-hit pitfall that functional components with hooks avoid entirely.
1Understanding Class Components
A class component extends React.Component and must implement a render() method returning JSX; state lives on this.state, initialized in the constructor and updated exclusively through this.setState(), and side effects tied to the component's lifecycle are handled through dedicated methods like componentDidMount and componentDidUpdate rather than a single unified hook. Class components require careful attention to this binding, since a method like an event handler loses its automatic this context unless explicitly bound in the constructor or written as an arrow function class property — a common, easy-to-hit pitfall that functional components with hooks avoid entirely.
If you inherit a codebase using class components, remember this.setState() merges the object you pass into existing state rather than replacing it entirely — unlike a functional component's useState setter, which fully replaces the state value unless you merge it yourself.
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
// <Greeting name="Ana" />2Practical Example
Here is a real-world application of Class Components showing how it is used in production React code.
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
this.handleClick = this.handleClick.bind(this);
}
handleClick() {
this.setState({ count: this.state.count + 1 });
}
render() {
return <button onClick={this.handleClick}>Count: {this.state.count}</button>;
}
}3Best Practices
Follow these guidelines when working with Class Components:
1. Bind event handler methods in the constructor, or write them as arrow function class properties, to avoid this being undefined when the handler is actually called
2. Remember this.setState() shallow-merges the provided object into existing state, unlike useState's setter, which replaces the value entirely
3. Recognize class components primarily when maintaining legacy code, since new components should be written as functional components with hooks instead
Tip: If you inherit a codebase using class components, remember this.setState() merges the object you pass into existing state rather than replacing it entirely — unlike a functional component's useState setter, which fully replaces the state value unless you merge it yourself.
class Greeting extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
// <Greeting name="Ana" />