Calling a React state setter queues an update and requests a render; it does not change the state value in the JavaScript code that is already running. React later renders the component with a new state snapshot, calculates the next UI, and commits any necessary changes to the screen.
Why does logging state right after a setter show the old value?
Each render of a component receives its own state snapshot. The component’s variables and the event handlers it creates use the values from that render. Calling a setter requests a future render, but it does not rewrite those values in the currently running handler.
As an Amazon Associate I earn from qualifying purchases.
function handleClick() {
setCount(count + 1);
console.log(count); // The value from this render
}
The log reads the count captured by the current render, so it shows the old value. React describes state as living outside the component function and being passed in as a snapshot for each render: State as a Snapshot.
Free tools Windows power users keep installed
One-click scans. No signup required.
If other code in the same handler needs the proposed next value immediately, calculate it yourself and use that local value:
#1 Best Overall
function handleClick() {
const nextCount = count + 1;
setCount(nextCount);
console.log(nextCount);
}
This does not make the setter synchronous; it simply gives your handler a value it calculated independently. The same snapshot behavior applies to asynchronous callbacks created during a render: they retain the values captured by that render.
What happens between the setter call and the screen update?
React describes a screen update as three stages: trigger, render, and commit. A state setter triggers an update. React then calls the relevant component functions to calculate what the UI should look like with the updated state, continuing through child components as needed. Finally, React commits the resulting changes to the screen. The distinction is explained in Render and Commit.
- Trigger: A setter requests an update.
- Render: React calls components to calculate the next UI from their props and state.
- Commit: React applies the necessary changes to the screen.
Rendering is the calculation; it is not itself the screen being changed. A render does not mean every DOM node is replaced, and the screen is not mutated at the instant the setter is called.
Why can several setter calls result in only one increment?
React queues updates and processes them after the current event handler has finished. It can batch updates from that handler so the interface does not show an intermediate state for each call. React’s guide on Queueing a Series of State Updates also notes that separate intentional events, such as separate clicks, are handled separately rather than batched together.
Rank #3
Direct values use the current render’s snapshot
In a handler, this sequence uses the same captured value for all three expressions:
setNumber(number + 1);
setNumber(number + 1);
setNumber(number + 1);
If number was 0 in that render, each call queues the replacement value 1. They do not successively read the result of the preceding call, so the final state is 1, not 3.
Rank #4
Updater functions use the preceding queued result
When each update depends on the value produced by the previous one, pass a function instead:
Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemssetNumber(n => n + 1);
setNumber(n => n + 1);
setNumber(n => n + 1);
React applies these updater functions in order. Each receives the result returned by the previous updater, so starting from 0, the queued updates produce 1, then 2, then 3.
Best Value
| Update form | Value it uses | Several calls in one handler |
|---|---|---|
setNumber(number + 1) |
The number snapshot from the current render |
Each call may queue the same replacement value |
setNumber(n => n + 1) |
The preceding queued state value | Each updater can build on the result before it |
What must an updater function do?
An updater should be pure: it calculates and returns the next state without causing side effects or calling a setter to update state from inside itself. React may call updater functions twice in development when Strict Mode is enabled, discarding one result to help reveal impure logic. The useState reference documents this behavior.
For class components, the related Component reference likewise describes setState as a request rather than an immediate command, and explains that queued updater functions calculate next state from prior state.
Why might the screen not update?
A setter call does not guarantee a visible change. React’s useState reference says it can ignore an update when the next value is identical to the current value according to Object.is. That check is about whether the state value changed; separately, a render calculates UI and a commit applies necessary screen changes.
Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minuteWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallQuick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




