ποΈ React Class Components Explained β Syntax, State, Lifecycle (2025)
π§² Introduction β Why Learn React Class Components?
Although functional components with Hooks are now the modern React standard, class components still exist in many production codebases. Knowing how class components work helps in maintaining legacy projects, understanding Reactβs evolution, and improving your overall React expertise.
π― In this guide, youβll learn:
- What class components are in React
- How to use
state
andprops
- Lifecycle methods like
componentDidMount
- Event handling and common use cases
ποΈ What is a Class Component?
A class component is an ES6 class that extends React.Component
and must contain a render()
method that returns JSX.
β Basic Syntax:
import React from 'react';
class Welcome extends React.Component {
render() {
return <h1>Hello, {this.props.name}!</h1>;
}
}
π It can accept props
, manage internal state
, and use lifecycle methods.
π§ Using State in Class Components
To add internal data (state), you define a constructor()
and initialize this.state
.
Example:
class Counter extends React.Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
render() {
return <button onClick={this.increment}>Count: {this.state.count}</button>;
}
}
π setState() is used to update the component state and trigger a re-render.
ποΈ Props in Class Components
Props are accessible via this.props
inside methods or JSX.
class Greeting extends React.Component {
render() {
return <h2>Hello, {this.props.username}!</h2>;
}
}
You can pass them like:
<Greeting username="Alice" />
π Lifecycle Methods in Class Components
Lifecycle methods are special functions React calls at specific stages of a componentβs life.
π Common Lifecycle Methods:
Method | Purpose |
---|---|
constructor() | Initialize state or bind methods |
render() | Return JSX to render UI |
componentDidMount() | Runs once after the component mounts |
componentDidUpdate() | Runs after updates to state/props |
componentWillUnmount() | Cleanup before unmounting (e.g., clear timers) |
Example:
class Timer extends React.Component {
componentDidMount() {
console.log('Component mounted');
}
componentWillUnmount() {
console.log('Component unmounted');
}
render() {
return <h2>Running...</h2>;
}
}
π― Event Handling in Class Components
React class components require binding or arrow functions to handle events properly.
Binding in constructor:
constructor(props) {
super(props);
this.handleClick = this.handleClick.bind(this);
}
Or use arrow functions:
handleClick = () => {
alert("Button clicked!");
};
render() {
return <button onClick={this.handleClick}>Click Me</button>;
}
π οΈ When to Use Class Components
β Use class components when:
- You’re working with legacy React projects
- Using React libraries that haven’t migrated to hooks
- You want to understand Reactβs historical lifecycle architecture
β οΈ Avoid for new projects unless compatibility is required.
π Class vs Functional β Code Comparison
ποΈ Class Component:
class Hello extends React.Component {
render() {
return <h2>Hello, {this.props.name}</h2>;
}
}
π― Functional Equivalent:
function Hello({ name }) {
return <h2>Hello, {name}</h2>;
}
βοΈ The functional version is cleaner, but the class version is still fully valid and useful in many codebases.
π Summary β Recap & Next Steps
React class components remain relevant in legacy apps. While Hooks in functional components offer a simpler, more modern approach, understanding class components is essential for full React mastery.
π Key Takeaways:
- Class components extend
React.Component
and userender()
- State is managed using
this.state
andthis.setState()
- Lifecycle methods offer fine control over component behavior
- Prefer functional components for new features, but maintain knowledge of class-based patterns
βοΈ Real-World Relevance:
Many enterprise and open-source apps still use class components. Knowing how to work with them ensures you can maintain and refactor large-scale legacy codebases.
β FAQ Section
β Are class components deprecated?
β
No. React still supports class components, but they are no longer the recommended approach for new development.
β Can I use hooks in class components?
β No. Hooks like useState
and useEffect
only work inside functional components.
β When should I use componentDidMount()
?
β
Use it to fetch data, start timers, or subscribe to events when the component is first rendered.
β How do I update the state in a class component?
β
Use this.setState()
:
this.setState({ key: newValue });
β Can I convert a class component to functional?
β
Yes. You can rewrite class components using functional syntax and use Hooks to replace lifecycle methods and state.
Share Now :