When Optimistic React Saves Finish in the Wrong Order

Switch email alerts on, then off again before the first save finishes. The screen reacts immediately to both clicks. A moment later, an older response arrives and switches the preference back on.
The interface has been responsive throughout. It has also finished confidently displaying the wrong thing.
React 19's optimistic state is useful here, but it does not decide which write the server should accept. I put together a two‑row preferences example to separate the user's latest intention, the server's accepted value, and the order in which replies reach the browser. Those can be three different stories about the same checkbox.
Start with a Failure You Can Replay
The broken version accepts every write and copies every response into the confirmed state. To expose it, the test sends two changes to the email preference, commits them in order, and delivers their replies in reverse order.
The server ends with alerts off. The browser receives the newer "off" reply first, then the older "on" reply, and finishes with alerts on. Reading the server again proves the mismatch. Checking only the final checkbox would leave us guessing which side was wrong.
My fixture controls commits and replies independently. It doesn't use a longer timeout for the first request and hope that the network obliges. The test waits until both requests have arrived, instructs the service to process each one, then releases the responses in the chosen order.
There is another failure to try. Let the older request reach the commit step last. A browser which ignores old responses might keep displaying the latest intention, whilst the server still accepts the stale write. A neat sequence number in React state cannot prevent that server‑side overwrite.
Put a Condition on the Write
For this exercise, each row has a boolean value and an increasing revision. A save carries the revision the user was editing:
const response = await fetch('/save', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'If-Match': `"${operation.expectedRevision}"`,
},
body: JSON.stringify(operation),
});The service compares that condition with the row's current revision at the point of committing the change. If they match, it updates the value and increments the revision. Otherwise, it returns 412 with the current row. This is the lost‑update protection described by HTTP's If-Match precondition.
Two edits made against revision zero cannot both succeed under this policy. Whichever is accepted first advances the revision; the other needs review. That is a deliberate conflict rule. Automatically changing the second request's expected revision and retrying it would make a different decision on the user's behalf.
For simple preferences, serialising saves per row can be a reasonable alternative: send the next change after receiving the previous result. Other rows can continue independently. A server condition is still useful when another tab or device can edit the same record, beyond the reach of that browser's queue.
Let Optimistic State Describe a Provisional View
The React 19 release introduces useOptimistic for the temporary view shown whilst an Action is pending. My component supplies its confirmed rows and a pure projection function:
const [optimistic, addOptimistic] = useOptimistic(
confirmed,
project,
);Each operation identifies its row, intended value, expected revision, and a local sequence number. The projection changes that row without mutating the confirmed input. Within the same revision, the later local operation supplies the visible intention.
When a newer authoritative revision arrives, an optimistic operation based on an older revision no longer overlays it. That rule matters if the user refreshes the saved values whilst a request is still pending. The projection runs against the new base rather than hanging on to a snapshot taken when the click happened.
This is the sort of state transition I discussed in my guide to useReducer: name the inputs and make the calculation testable without a browser. The projection tests use explicit expected states, including an unchanged second row. They do not just call the reducer twice and congratulate it for agreeing with itself.
The optimistic update is dispatched inside an async Action. Confirmed values and per‑operation outcomes remain application state. React manages the provisional rendering; the application still owns the meaning of a successful response.
Accept Newer Evidence Without Reviving Older Errors
Responses carry their accepted revision. The corrected client refuses to replace a confirmed row with a lower revision:
if (incoming.revision < current.revision) {
return current;
}That comparison protects the browser from an older snapshot arriving late. The server precondition protects the stored value from a stale write arriving late. Both checks earn their place because they operate at different points in the journey.
Errors need identity too. A rejected old operation should not replace a newer operation's successful status with a warning about the wrong attempt. The example attaches errors to their row and operation sequence. A conflict affecting the latest intention remains visible, with the authoritative value available for review.
I also retain pending operations per row. One global "saving" flag cannot tell us that email has settled whilst the weekly digest is still waiting. The controls keep their identity, so a reply for the email row does not steal focus from the digest checkbox.
A Failed Reply Can Hide an Accepted Save
One scenario commits the change, sends an incomplete response, and closes the connection. The browser cannot parse the reply. The server has nevertheless saved the preference.
The interface calls this outcome unknown, temporarily prevents another change to that row, and offers a refresh of the saved values. Once that read succeeds, the user has an authoritative starting point again. A malformed success response gets the same cautious treatment; it is not allowed to insert undefined values into the confirmed state.
I covered durable operation receipts and retry recovery in my offline form article. This smaller example has no durable outbox or receipt store. Its refresh is sufficient to demonstrate reconciliation of a boolean preference, but it does not establish whether an arbitrary payment or append‑only operation can safely be repeated.
Check the Server as Well as the Screen
My optimistic‑write lab includes both variants, the programmable service, pure projection checks, and browser scenarios. The cases cover reversed replies, an older request attempting to commit late, a latest‑edit conflict, an old failure after newer success, an unrelated pending row, a lost reply, and an authoritative refresh during an optimistic update.
For each case, read the final server value independently and compare it with the interface. Check the pending and error text as well as the checkbox. Keeping focus in the right place matters too; a technically correct save should not make the user hunt for the control they were just using.
Optimism is useful because it lets the interface respond before the server finishes. The response still has to be checked when it arrives.
Postscript
November 2026: React's current transition guidance distinguishes custom async Transitions from higher‑level APIs such as useActionState and form Actions, which manage request ordering. My lab now runs the scenarios against React 19.0.0 and 19.3.1. Before building a custom queue, check whether one of those higher‑level APIs fits the interaction; server‑side conflict and uncertain‑outcome decisions still need an explicit policy.