setState is a method used in React class components to update the component’s state that triggers a re-render with the updated values. All the React components can have a state associated with them. The state of a component can change either due to a response to an action performed by the user or an event triggered by the system.
We use the setState() method to change the state object. It ensures that the component has been updated and calls for re-rendering of the component.
React JS setState
setState in React is an asynchronous call means if a synchronous call gets called it may not get updated at the right time like to know the current value of an object after an update using setState it may not give the updated value on the console. To get some behavior for synchronous need to pass function instead of object to setState.
To deepen your knowledge of state management and other advanced React concepts, consider enrolling in the React JS Course offered by GeeksforGeeks. This course provides a comprehensive overview of state management techniques, including hooks like useState and useReducer, allowing you to manage state more effectively in functional components. With hands-on projects and practical applications, you’ll gain the skills necessary to build robust React applications.
Syntax:
We can use setState() to change the state of the component directly as well as through an arrow function.
setState({ stateName : updatedStateValue })
// OR
setState((prevState) => ({
stateName: prevState.stateName + 1
}))Steps to Create React Application
Step 1: Create a React application using the following command:
npx create-react-app foldername
Step 2: After creating your project folder i.e. foldername, move to it using the following command:
cd foldername
Project structure:

Approach 1: Updating single attribute
We set up our initial state value inside constructor function and create another function updateState() for updating the state. Now when we click on the button, the latter gets triggered as an onClick event which changes the state value. We perform setState() method in our updateState() function by writing:
this.setState({
greeting : 'GeeksForGeeks welcomes you !!'
}) As you can see, we are passing an object to setState(). This object contains the part of the state we want to update which, in this case, is the value of greeting. React takes this value and merges it into the object that needs it. Itâs just like the button component asks what it should use for updating the value of greeting and setState() responds with an answer.
JavaScript
// Filename - App.js
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
// Set initial state
this.state = {
greeting:
"Click the button to receive greetings",
};
// Binding this keyword
this.updateState = this.updateState.bind(this);
}
updateState() {
// Changing state
this.setState({
greeting: "GeeksForGeeks welcomes you !!",
});
}
render() {
return (
<div>
<h2>Greetings Portal</h2>
<p>{this.state.greeting}</p>
{/* Set click handler */}
<button onClick={this.updateState}>
Click me!
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Approach 2: Updating multiple attributes
The state object of a component may contain multiple attributes and React allows using setState() function to update only a subset of those attributes as well as using multiple setState() methods to update each attribute value independently.
We set our initial state by initializing three different attributes, and then we create a function updateState() which updates its value whenever it is called. Once again this function gets triggered as an onClick event and we get the updated values of our states at the same time.
JavaScript
// Filename - App.js
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
// Set initial state
this.state = {
test: "Nil",
questions: "0",
students: "0",
};
// Binding this keyword
this.updateState = this.updateState.bind(this);
}
updateState() {
// Changing state
this.setState({
test: "Programming Quiz",
questions: "10",
students: "30",
});
}
render() {
return (
<div>
<h2>Test Portal</h2>
<p>{this.state.test}</p>
<p>{this.state.questions}</p>
<p>{this.state.students}</p>
{/* Set click handler */}
<button onClick={this.updateState}>
Click me!
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Approach 3: Updating state values using props
We set up an array of strings testTopics as props for our component. A function listOfTopics is created to map all the strings as list items in our state topics. The function updateState gets triggered and sets the topics to list items. When we click on the button, we get updated state values. This method is well-known for handling complex data and updating the state very easily.
JavaScript
// Filename - App.js
import React, { Component } from "react";
class App extends Component {
static defaultProps = {
testTopics: [
"React JS",
"Node JS",
"Compound components",
"Lifecycle Methods",
"Event Handlers",
"Router",
"React Hooks",
"Redux",
"Context",
],
};
constructor(props) {
super(props);
// Set initial state
this.state = {
testName: "React js Test",
topics: "",
};
// Binding this keyword
this.updateState = this.updateState.bind(this);
}
listOfTopics() {
return (
<ul>
{this.props.testTopics.map((topic) => (
<li>{topic}</li>
))}
</ul>
);
}
updateState() {
// Changing state
this.setState({
testName: "Test topics are:",
topics: this.listOfTopics(),
});
}
render() {
return (
<div>
<h2>Test Information</h2>
<p>{this.state.testName}</p>
<p>{this.state.topics}</p>
{/* Set click handler */}
<button onClick={this.updateState}>
Click me!
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Approach 4: Updating the state using its previous value
We create an initial state count having a value of 0. The function updateState() increments the present value of the state by 1 whenever it is called. This time we use setState() method in an arrow function by passing prevState as a parameter. The current value of the state is accessed with prevState.stateName which gets incremented by 1 whenever we press the button. This method is really useful when we are setting a value in the state in such a way that it depends on its previous value. For example, toggling a boolean (true/false) or incrementing/decrementing a number.
JavaScript
// Filename - App.js
import React, { Component } from "react";
class App extends Component {
constructor(props) {
super(props);
// Set initial state
this.state = {
count: 0,
};
// Binding this keyword
this.updateState = this.updateState.bind(this);
}
updateState() {
// Changing state
this.setState((prevState) => {
return { count: prevState.count + 1 };
});
}
render() {
return (
<div>
<h2>Click Counter</h2>
<p>
You have clicked me {this.state.count}{" "}
times.
</p>
{/* Set click handler */}
<button onClick={this.updateState}>
Click me!
</button>
</div>
);
}
}
export default App;
Step to Run Application: Run the application using the following command from the root directory of the project:
npm start
Output: Now open your browser and go to http://localhost:3000/, you will see the following output:

Summary
React setState provides a way to manage the state in React class components asynchronously and update the UI based on user interactions and other changes.
Similar Reads
ReactJS setState()
setState is a method used in React class components to update the component's state that triggers a re-render with the updated values. All the React components can have a state associated with them. The state of a component can change either due to a response to an action performed by the user or an
7 min read
ReactJS State
State is a fundamental concept in ReactJS that plays an important role in building interactive and dynamic web applications. In this article, we will dive deep into what state is in ReactJS, how to use it and explore advanced techniques for managing state in large applications. Table of Content What
4 min read
React useState Hook
The useState hook is an alternative to the useReducer hook that is preferred when we require the basic update. useState Hooks are used to add the state variables in the components. For using the useState hook we have to import it in the component. Syntax const [state, setState] = useState(initialSta
4 min read
ReactJS useUndoState hook
The useUndoState hook is a custom hook provided by the Rooks package for React. It is similar to the useState hook in addition to undo functionality. Arguments: initialValue: It is of the type boolean that describes the initial value of the state. Its default value is false.Options: It is of the typ
2 min read
ReactJS Refs
ReactJS Refs are used to access and modify the DOM elements in the React Application. It creates a reference to the elements and uses it to modify them. Table of Content Creating refs in ReactAccessing Refs in ReactWhy useRef over createRef in Function Components?When to use refsWhen not to use refs
4 min read
ReactJS State vs Props
In React, State allows components to manage and update internal data dynamically, while Props enables data to be passed from a parent component to a child component. Understanding their differences and use cases is essential for developing efficient React applications. State in ReactState is a built
4 min read
React Native State
In React Native, for data that is going to change, we have to use state. You can think of it as a variable. It allows us to store data and also change it when we want. Whenever you define a state, you need to provide an initial value for it. After that, you can use setState function provided by Reac
3 min read
What is useState() in React ?
The useState() is a Hook that allows you to have state variables in functional components . so basically useState is the ability to encapsulate local state in a functional component. React has two types of components, one is class components which are ES6 classes that extend from React and the other
2 min read
ReactJS Props - Set 1
The react props refer to properties in react that are passed down from parent component to child to render the dynamic content. Till now we have worked with components using static data only. In this article, we will learn about react props and how we can pass information to a Component. What are Pr
5 min read
ReactJS Props - Set 2
In our previous article ReactJS Props - Set 1 we discussed props, passing and accessing props, passing props from one component to another, etc. In this article, we will continue our discussion on props. So, what if we want to pass some default information using props to our components? React allows
4 min read