# React's setState is asynchronous

By Saurabh Kumar · TIL note, Nov 8, 2023 · Tags: javascript

Source: https://saurabh-kumar.com/til/1699444800/

React's `setState` is asynchronous: read `this.state` right after calling it and you may get the old value. To act on the new state, pass a callback, or use `componentDidUpdate` (which also fires on prop changes). From [React gotchas](https://daveceddia.com/react-gotchas/).
```js
this.setState({ name: 'Jon' }, () => {
  // runs after the state update and re-render
});
```
