React Reference ยท Components and Special Elements
this.state and setState in React classes
Reference for class component state: this.state, setState, shallow merging, the updater function form, callbacks and batching, with common mistakes.
In a class component, state is a single object stored on this.state. You set its initial value once (a class field or the constructor) and change it only by calling this.setState. Each call tells React to merge your changes into the state and render the component again.
Two differences from useState matter most. First, setState merges: you pass only the keys that changed and the rest are kept. Second, this.state is a snapshot for the current render, so it does not change immediately after you call setState.
class Form extends Component { state = { name: '', agreed: false }; render() { return <input onChange={(e) => this.setState({ name: e.target.value })} />; } }
Try it
Press the first button: a goes up by 1, not 3, because all three calls read the same snapshot of this.state.a. The second button adds 3 because each updater receives the result of the one before. The callback runs after the update is applied, so it sees b as 3.
Syntax
this.state = { key: value }; // only in the constructor state = { key: value }; // class field this.setState(partialState); this.setState((prevState, props) => partialState); this.setState(update, callback);
| Name | Description |
|---|---|
partialState |
An object whose keys are merged into the existing state. Missing keys are left alone. |
prevState, props |
The state and props that the update is based on, including earlier queued updates. |
callback |
Optional. Runs after the update is applied and the component re-rendered. |
Returns: undefined.
Special case: returning null from an updater function (or passing null) means "no change", and React skips the re-render.
Usage notes
- Never assign to
this.stateoutside the constructor or field.this.state.count = 5changes nothing on screen and corrupts the snapshot. Always callsetState. - Merging is shallow. Nested objects are replaced, not merged:
setState({ user: { name: 'x' } })discards other keys ofuser. Spread them:{ user: { ...this.state.user, name: 'x' } }. - Use the updater form whenever the new value depends on the previous state or props.
- Batching. Updates inside event handlers, effects, timers and promises are batched in React 18 and later, so several calls cause one render.
- State is not immediately updated. Read the new value in
componentDidUpdateor in thesetStatecallback, not on the next line. - Only store what changes UI. Values that never affect rendering (timer ids, DOM nodes) belong on instance fields (
this.timer), not in state. - Do not derive state from props by copying them into state; compute during
render. If you must react to a prop change, usecomponentDidUpdatewith a comparison, or reset by giving the component a newkey. - Do not call
setStateinrenderor the constructor. It loops or warns. - Hook comparison. The
useStatesetter replaces the value instead of merging, and a function component can hold many independent state variables. A class holds one object.