Skip to content
tezvyn:
Menu

Optimistic UI updates for actions

Source: interviewHardHow cards are made

Summary

perceived-performance technique with rollback.

Key points

update UI immediately on the assumption of success, send the request, then confirm or roll back on failure.

Watch out for

applying optimistic state without any rollback or error path.

What's really being asked

Whether you can trade certainty for responsiveness safely, which hinges entirely on a correct rollback path, not just the happy case.

The full answer

Optimistic UI updates the interface immediately, assuming the mutation will succeed, so interactions feel instant despite network latency. The essential steps are: capture the current state as a snapshot, apply the optimistic change locally, issue the server request, and then reconcile. On success you confirm, optionally replacing optimistic values with authoritative server data. On failure you restore the snapshot and surface a brief, non-blocking error so the user understands the action did not stick. React's useOptimistic hook formalizes this by deriving optimistic state that automatically reverts when the underlying action settles, and Apollo and React Query offer optimisticResponse and onMutate with rollback hooks.

The mistakes people make

Applying the optimistic change with no rollback, so a failed request leaves a like that the server never recorded; not snapshotting prior state so revert is impossible; double-counting when a slow real response also increments; or blocking the UI with a spinner, which defeats the purpose.

What usually comes next

How does useOptimistic differ from manual useState rollback? How do you reconcile the optimistic value with the server's authoritative count? How do you debounce rapid toggles?

A concrete example

Tapping like immediately flips liked to true and increments the count from 41 to 42, then POSTs to the server. If the POST succeeds, the optimistic state stands or is replaced by the returned count. If it fails, the code restores liked to false and the count to 41 and shows 'Couldn't like, try again'. With useOptimistic, the optimistic 42 reverts automatically once the failed action resolves, without manual snapshot bookkeeping.

Interview question

What single step most distinguishes a correct optimistic update from a naive one when the server request fails?

  • a.Clearing the entire screen's state and forcing a full data refetch
  • b.Reverting to the snapshot of state captured before the optimistic changeCorrect
  • c.Disabling the button permanently so the user cannot retry the action
  • d.Re-sending the same request repeatedly until the server eventually accepts it
Why?

Optimistic UI is only correct if a failure rolls back to the pre-change snapshot, keeping the UI consistent with the server. Disabling retries, infinite resending, or nuking all state are unnecessary and harmful, so they are wrong.

Just read this? Test yourself on what you have been reading.

Read the original → react.dev

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles