What and Why?
A class based component is a React component written as a JavaScript class that extends React.Component. Before hooks were added to React, classes were the only way to give a component its own state or to run code when it appeared on or left the screen. Today new code is usually written with function components and hooks, but class components still work, are fully supported, and fill a lot of existing codebases. Knowing how they work lets you read, maintain and gradually modernise that code.
What a class component looks like
A class component must define a render() method that returns JSX. Props arrive on this.props, state lives in this.state, and you change state by calling this.setState(), which tells React to render again.
import { Component } from 'react';
class Counter extends Component {
state = { count: 0 };
increment = () => {
this.setState(prev => ({ count: prev.count + 1 }));
};
render() {
return (
<button onClick={this.increment}>
{this.props.label}: {this.state.count}
</button>
);
}
}
export default Counter;
Why you still need to learn them
- Existing projects. Many production apps were written when classes were the standard, and you will be asked to fix bugs or add features in them.
- Error boundaries. A component that catches rendering errors in its children must still be a class, because the methods
componentDidCatchandstatic getDerivedStateFromErrorhave no hook equivalent. - Understanding the lifecycle. Methods such as
componentDidMount,componentDidUpdateandcomponentWillUnmountname the moments a component goes through. Seeing them spelled out makes it easier to understand whatuseEffectis doing for you. - Older tutorials and libraries. Documentation and answers you find online often use class syntax.
It also helps to know what classes do not offer. A class cannot call hooks, so you cannot use useState, useContext or a custom hook inside one. If a class component needs logic that only exists as a hook, the usual approach is to wrap it in a small function component that calls the hook and passes the result down as props.
Mapping class ideas to hooks
| Class component | Function component |
|---|---|
this.state and this.setState() | useState |
componentDidMount | useEffect with an empty dependency array |
componentDidUpdate | useEffect with dependencies |
componentWillUnmount | The cleanup function returned from useEffect |
this.props | The function's props parameter |
The confusing part: this
The biggest source of bugs in class components is the this keyword. If you write a normal method such as handleClick() { this.setState(...) } and pass it as onClick={this.handleClick}, React calls it without the component as its context, so this is undefined and you get "Cannot read properties of undefined". The fix is either to bind the method in the constructor or to define it as an arrow function class field, as increment does above.
A second trap is changing state directly with this.state.count = 5. React does not notice the change and the screen does not update. Always go through setState().
Where this leaves you
Write new components as functions, but be comfortable reading classes. Recognise render(), this.props, this.state and setState(), watch out for unbound methods, and remember that error boundaries are the one place a class is still required.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.