Before React 16.8 and the introduction of Hooks, Class Components were the only way to track state and lifecycle events. Knowing class components is essential when working with existing legacy codebases.
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
render() {
return (
<button onClick={() => this.setState({ count: this.state.count + 1 })}>
Count: {this.state.count}
</button>
);
}
}