In class-based React components, state is used to store and manage data that can change over time. When the state changes, the component automatically re-renders to show the latest UI.
State is defined using this.state (usually inside the constructor) and updated using this.setState(). It plays a role similar to useState in functional components.
State = Dynamic, interactive data
this.state = {"{}"}.this.setState(), never by changing this.state directly.setState().A typical class component with state:
// Basic skeleton of a class component with state
class MyComponent extends React.Component {
constructor(props) {
super(props);
this.state = {
value: 0
};
}
render() {
return <h2>Value: {this.state.value}</h2>;
}
}
constructor(props) runs once when the component is created.this.state holds the initial values.this.state.value is read inside render() to display in the UI.In class-based components, state is used to store and manage data that changes over time. It allows components to re-render dynamically when data updates, similar to useState in functional components.
You define state inside the constructor using this.state and update it using this.setState().
// Class-based counter using this.state and this.setState()
import React, { Component } from "react";
class Counter extends Component {
constructor(props) {
super(props);
this.state = { count: 0 };
}
increment = () => {
this.setState({ count: this.state.count + 1 });
};
decrement = () => {
this.setState({ count: this.state.count - 1 });
};
reset = () => {
this.setState({ count: 0 });
};
render() {
return (
<div>
<h2>Count: {this.state.count}</h2>
<button onClick={this.increment}>Increment</button>
<button onClick={this.decrement}>Decrement</button>
<button onClick={this.reset}>Reset</button>
</div>
);
}
}
export default Counter;
The component shows the current count and three buttons: Increment, Decrement, and Reset.
this.state.count holds the current value.this.setState() updates the state and triggers a re-render.You usually define initial state inside the constructor:
// Initialize multiple state fields inside the constructor
constructor() {
super();
this.state = {
name: "Guest",
age: 20
};
}
Always call super() before using this inside the constructor, otherwise this will be undefined.
setState()State must be updated using this.setState() — never modify this.state directly, otherwise React will not re-render the UI.
// Correct vs incorrect ways to update state
this.setState({ count: this.state.count + 1 });
// ❌ Wrong: direct mutation (will not trigger re-render)
this.state.count = this.state.count + 1;
The setState() method merges the new state with the existing state and then re-renders the component. Directly changing this.state bypasses React’s update system.
When the next state depends on the previous one, use the callback version of setState() to avoid bugs caused by asynchronous updates.
// Safer way to update based on previous state
this.setState((prevState) => ({
count: prevState.count + 1
}));
React may batch multiple state updates together. Using prevState guarantees that you always get the most recent value when calculating the next state.
// Class component with multiple state fields
class Profile extends Component {
constructor() {
super();
this.state = {
name: "Riya",
city: "Pune",
followers: 100
};
}
updateFollowers = () => {
this.setState({ followers: this.state.followers + 10 });
};
render() {
return (
<div>
<h3>Name: {this.state.name}</h3>
<p>City: {this.state.city}</p>
<p>Followers: {this.state.followers}</p>
<button onClick={this.updateFollowers}>+10 Followers</button>
</div>
);
}
}
React automatically merges the new state with the existing one. When calling this.setState({"{"} followers: ... {"}"}), only the followers property changes — name and city remain the same.
this.state directly — React won’t re-render.setState() is asynchronous — don’t rely on the new value immediately after calling it.this.state = {"{}"}.this.setState() to update values instead of directly mutating this.state.setState() when the next state depends on the previous state.increment = () => {"{}"}) to auto-bind this without using .bind(this).ScoreTracker with an initial score of 0.Increase, Decrease, and Reset the score.this.state.score.setState() when changing the score (for example when incrementing multiple times).Goal: Learn how to define, update, and render state in class-based React components using this.state and this.setState() to build interactive UIs.