Class Components
Learn the older class-based component syntax, this.props and this.state, lifecycle methods, and why modern React favors function components instead.
Introduction
Before Hooks existed, class components were the only way to give a component internal state or respond to lifecycle events like "the component just appeared on screen." Function components were limited to simple, static UI. That is why older React code — and some libraries and tutorials still in use today — is full of classes.
You will rarely write a brand-new class component today, but understanding the syntax is essential for reading legacy codebases, older Stack Overflow answers, and some third-party libraries. This lesson walks through class syntax conceptually, without expecting you to use it going forward.
- The older class-based component syntax using React.Component.
- How this.props works inside a class component.
- What this.state and this.setState() do, at a conceptual level.
- The names and rough purpose of common lifecycle methods.
- Why modern React codebases prefer function components with Hooks.
Writing a Class Component
A class component is a JavaScript class that extends React.Component and defines a render() method. Instead of returning JSX directly from a function, the JSX is returned from render().
class Welcome extends React.Component {
render() {
return <h1>Hello</h1>;
}
}Helloextends React.Component
Every class component inherits from React's base Component class, which provides its core behavior.
render() Method
This required method returns the JSX describing what the component displays.
this Keyword
Class components rely heavily on this to access props, state, and methods.
Older Syntax
This style predates Hooks and is now considered legacy for new code.
this.props in Class Components
Just like function components receive props as an argument, class components receive props too — but they are accessed through this.props rather than a function parameter.
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
// Usage
<Welcome name="Amol" />Hello, Amol!Notice the "this." prefix is required in a class component, whereas a function component could simply destructure { name } directly from its parameter. This is one of the small but constant syntax differences between the two styles.
this.state and this.setState()
Class components can hold their own internal data using this.state, initialized in a constructor. To update it, you never modify this.state directly — instead you call this.setState(), which tells React to update the value and re-render the component. The full concept of state is covered in depth in the next lesson; here the focus is just on the class syntax used to work with it.
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
handleClick = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return (
<button onClick={this.handleClick}>
Count: {this.state.count}
</button>
);
}
}Writing this.state.count = this.state.count + 1 directly will not trigger a re-render and breaks React's update process. Always go through this.setState() so React knows the data changed.
Lifecycle Methods (Brief Overview)
Class components expose special methods React calls automatically at specific moments in a component's life — when it first appears, when it updates, and when it is removed. A dedicated lifecycle lesson later in this course covers these in full detail; for now, just recognize their names and general purpose.
componentDidMount
Runs once, right after the component first appears on screen. Common for data fetching.
componentDidUpdate
Runs after the component re-renders due to changed props or state.
componentWillUnmount
Runs right before the component is removed from the screen. Common for cleanup like clearing timers.
Why Modern React Prefers Function Components
Hooks (introduced in 2019) gave function components the same abilities class components had — state with useState, lifecycle-style behavior with useEffect — but with dramatically less boilerplate and none of the "this" binding pitfalls that commonly tripped up class component authors.
Why Classes Fell Out of Favor for New Code
- Class components require a constructor and "super(props)" just to set up initial state.
- Event handler methods often need manual binding of "this" (or arrow function class fields) to avoid "this is undefined" bugs.
- Sharing stateful logic between class components usually required awkward patterns like higher-order components or render props.
- Function components with Hooks accomplish the same things with simpler, more readable, more testable code.
Recognizing class syntax matters because production codebases, especially older ones, still contain class components — and you will need to read, maintain, or gradually migrate that code even if you never write a new class component yourself.
Example: A Small Class Component
Here is a self-contained class component combining props, state, and a lifecycle method, showing how these pieces fit together.
class Greeting extends React.Component {
componentDidMount() {
console.log('Greeting mounted for ' + this.props.name);
}
render() {
return <h2>Hello, {this.props.name}!</h2>;
}
}
// Usage
<Greeting name="Priya" />Hello, Priya!
(console: "Greeting mounted for Priya")Common Mistakes
- Forgetting to call super(props) inside the constructor, which breaks access to this.props.
- Modifying this.state directly instead of calling this.setState().
- Forgetting the render() method entirely — a class component without it will throw an error.
- Writing new class components in a modern codebase when a simpler function component with Hooks would do the same job.
Best Practices
- Recognize class syntax well enough to read legacy code confidently, even if you do not write it.
- When maintaining an old class component, avoid mixing in unrelated new patterns — keep changes minimal and consistent.
- Prefer function components with Hooks for any new component you create.
- If migrating a class component to a function component, tackle one component at a time and test thoroughly.
- Always call super(props) as the first line of any class component constructor.
Frequently Asked Questions
Are class components deprecated?
No, React still fully supports class components and has no plans to remove them. They are simply no longer the recommended style for new code.
Can function and class components be used together in the same app?
Yes. A function component can render a class component and vice versa. Many real codebases mix both during a gradual migration.
Why do I need super(props) in the constructor?
It calls the parent React.Component constructor, which is required before you can use this.props inside the constructor. Without it, this.props would be undefined there.
Is this.setState() synchronous?
Not necessarily — React may batch multiple setState calls together for performance, so you should not assume this.state reflects the new value immediately after calling it.
Do I need to memorize all the lifecycle methods now?
No. This lesson only introduces their names and rough purpose. A dedicated lesson on the component lifecycle later in this course covers each one in full detail.
Key Takeaways
- Class components extend React.Component and define a render() method that returns JSX.
- Props are accessed via this.props, and internal data is accessed via this.state.
- State must always be updated through this.setState(), never modified directly.
- Lifecycle methods like componentDidMount, componentDidUpdate, and componentWillUnmount run at specific points in a component's life.
- Modern React favors function components with Hooks, but recognizing class syntax remains important for legacy code.
Summary
Class components were React's original way to add state and lifecycle behavior to a component, using this.props, this.state, this.setState(), and named lifecycle methods. While modern React favors function components with Hooks for their simplicity, class syntax still appears often enough in real-world code that recognizing it is a valuable skill.
In this lesson, you learned the class component syntax, how props and state are accessed with this, a brief overview of lifecycle methods, and why the React community shifted toward function components. Next, you will study props in much greater depth — the primary way data flows through a React application.
- You can read and understand a class component.
- You know how this.props and this.state work conceptually.
- You recognize common lifecycle method names and their rough purpose.
- You are ready to study props in depth next.