Skip to main content
InteractionLevel 3

Optimistic UI

乐观更新

WebMobileDesktopDifficulty: Advanced

Update the interface to "already succeeded" before the server answers, and roll back if it fails.

01

Live Demo

Actually clickable

A day on the coast

Not liked

Tap it: the icon lights up instantly (no waiting on the server). Turn on "Simulate a failure" and tap again to watch it roll back.

What you just saw:The heart icon lighting up the instant you tap it on Xiaohongshu, a sent message appearing immediately in a WeChat thread with a sending marker, a todo item striking through the moment it is checked.

02

What it actually is

Optimistic UI is a qualitative leap in feel: tapping Like fills the icon instantly instead of spinning for two seconds. The price is that you must handle rollback, which means preparing exactly how you will undo the optimistic update.

optimistic uioptimistic updateoptimistic renderinginstant feedbackrollback on failure
03

Anatomy · What it's made of

  1. Instant Apply立即生效

    The UI changes first

  2. Pending Marker处理中标记

    Semi-transparent or a small spinner

  3. Rollback回滚

    Restored when the request fails

  4. Error Toast错误提示

    Explains what happened

04

When to Use

When it fits, and when it doesn't
Optimistic UI usage recommendations
SituationUse it?
The action has a high success rate (>95%)Yes
Failure can be rolled back cheaplyYes
Waiting would make the interface feel sluggishYes
The action is irreversible (transfers, payments)No
The failure rate is highDepends

Irreversible actions like transfers and payments must wait for server confirmation.

A rollback has to be surfaced: rolling back silently makes users think their action was lost.

05

Similar Patterns

Don't mix these up
06

How to tell AI

Three levels of prompt

Add the component name and specifics

When the user taps Like, light up the icon immediately; if the API call fails, roll it back to unliked and show a toast saying "Action failed. Please retry."

07

Don't just say this to AI

Bad → Better

Don't say this to AI

Update the interface first, roll back on failure.

Say this instead

Implement Optimistic UI: mutate local state first and record prevState, mark the element as pending during the request to prevent double clicks, and on failure restore the state plus show a "Action failed" toast with a "Retry" button — the rollback must be visible to the user, never silent.

08

Share this card

Social-ready

Turn this pattern into a 3:4 or 1:1 knowledge card and export it as PNG right in the browser. No backend needed.