Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix Now×
Skip to content
World desk3 min

React State Updates: Why Does the Setter Show the Old Value?

A React state setter queues an update—it does not rewrite the value captured by the current render. Here’s how snapshots, batching, rendering, and commits fit together.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

If other code in the same handler needs the proposed next value immediately, calculate it yourself and use that local value:

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.

  1. Trigger: A setter requests an update.
  2. Render: React calls components to calculate the next UI from their props and state.
  3. 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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

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.

Updater functions use the preceding queued result

When each update depends on the value produced by the previous one, pass a function instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
setNumber(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.

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
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

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.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Outdated Drivers Are Slowing You DownFree scan - exact matches
Windows Errors? Fix Them Before They SpreadFree repair scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.