How to update the state of react components using callback? Last Updated : 12 Sep, 2021 Comments Improve Suggest changes Like Article Like Report The state is mutable in react components. To make the React applications interactive we almost use state in every react component. The state is initialized with some value and based on user interaction with the application we update the state of the component at some point in time using setState method. setState method allows to change of the state of the component directly using JavaScript object where keys are the name of the state and values are the updated value of that state. Often we update the state of the component based on its previous state. In these cases it is always advised to update the state using a callback-based approach because using this approach, it is ensured that previously states are fully updated and now we update the state based on its previously updated value. It is community advisable to use a callback-based approach to update the state using setState because it solves lots of bugs upfront that may occur in the future. Syntax this.setState(st => { return( st.stateName1 = state1UpdatedValue, st.stateName2 = state2UpdatedValue ) }) Example 1: This Example illustrates how to update state using a callback-based approach index.js : JavaScript import React from 'react' import ReactDOM from 'react-dom' import App from './App' ReactDOM.render(<App />, document.querySelector('#root')) app.js : JavaScript import React, { Component } from 'react' class App extends Component{ constructor(props){ super(props) // initialize count state this.state = {count : 0} // bind this this.handleClick = this.handleClick.bind(this) } // function to run after click handleClick(){ // changing state using callback this.setState(st => { // count is incremented +1 time // based on its previous value return st.count += 1 }) } render(){ return( <div> <h3>Number : {this.state.count}</h3> <button onClick={this.handleClick}> Increment count </button> </div> ) } } export default App Output : Example 2: index.js : JavaScript import React from 'react' import ReactDOM from 'react-dom' import App from './App' ReactDOM.render(<App />, document.querySelector('#root')) app.js : JavaScript import React, { Component } from 'react' class App extends Component{ static defaultProps = { name : ['John', 'Alex', 'Bob'] } constructor(props){ super(props) // initialize count state this.state = {msg : 'Hi There', count:0} // bind this this.handleClick = this.handleClick.bind(this) } // function to run after click handleClick(){ // changing state using callback this.setState(st => { return( st.msg = `${st.msg}, ${this.props.name[st.count]}`, st.count += 1 ) }) } render(){ return( <div> <h3>Greetings!</h3> <p>{this.state.msg}</p> <button onClick={this.handleClick}> Say greeting to employees! </button> </div> ) } } export default App Output : Comment More infoAdvertise with us Next Article How to update the state of react components using callback? H hunter__js Follow Improve Article Tags : JavaScript Web Technologies ReactJS Similar Reads How to update the State of a component in ReactJS ? To display the latest or updated information, and content on the UI after any User interaction or data fetch from the API, we have to update the state of the component. This change in the state makes the UI re-render with the updated content. Prerequisites: NPM & Node.jsReact JSReact StateReact 3 min read How to change the state of react component on click? To change the state of the React component is useful when you are working on a single page application, it simply replaces the content of the existing component for the user without reloading the webpage. We have to set initial state value inside constructor function and set click event handler of t 2 min read How to wait for a ReactJS component to finish updating ? To wait for a ReactJS component to finish updating, we use a loading state in our react application by use of the conditional rendering of the component. ApproachThis can be achieved by the use of the useState and useEffect hooks in the functional components. With the help of the state, we make sure 2 min read How to share state across React Components with context ? The React Context Provides simple and efficient way to share state across the React components. In this article, we will see how to share state across React Components with Contect API. Prerequisites:React Context React useState HookApproachTo share state across React Components with Context we will 4 min read How re-render a component without using setState() method in ReactJS ? In React, to re-render a class-based component with an updated state we generally use the setState() method. But instead, we can use other methods to re-render a component without using setState(). Prerequisites:Â NPM & Node.jsReact JSReactJS propsReactJS forceUpdate() methodApproaches to re-rend 3 min read Like