0
Challenge · 29Mutations & Auth

Optimistic UI (useOptimistic)

Mutationsused in ~62% of apps

Slow networks make every click feel broken. useOptimistic lets you paint the result instantly — but the part that separates a senior from a tutorial-follower is what happens when the server says no. If you only learned the happy path, your UI lies to users.

Core idea. `useOptimistic(realState, reducer)` gives you a temporary, overlay value that you show the moment an action starts. It is tied to the pending transition: when the Server Action settles, React THROWS THE OVERLAY AWAY and snaps back to the authoritative state — so on error it rolls back automatically. Your job is to make the server the source of truth and to surface the failure, not to keep the lie on screen.
Playground, this isn't a screenshot
<Thread />in sync
Shipped the feed
Optimistic next

Hit send: the message shows instantlyas "sending…" while the Action runs. On success it settles into the real thread; with server will fail on, the Action throws and React auto-reverts the optimistic bubble — you never wrote the rollback.

↑ play a little first, then dig in