Skip to main content

React useOptimistic Playground

Explore React 19's useOptimistic hook with a live todo list demo. Add items, toggle completion, and watch optimistic state vs. actual server state — with configurable delay and failure rates to trigger rollbacks.

✓ 60K+ developers trust DevPlaybook ✓ Runs 100% in your browser ✓ No sign-up required

Frequently Asked Questions

What is useOptimistic in React 19?▼

useOptimistic is a React 19 hook that lets you show an optimistic (predicted) UI state immediately while a background async operation runs. You provide the real state and a reducer function. React derives the optimistic state from your real state plus any pending optimistic updates, automatically reverting when the operation completes.

How does automatic rollback work?▼

When you call addOptimistic(data), React temporarily overlays the optimistic change on top of your real state. If the async action succeeds and you call setRealState(), React merges the real result. If the action throws an error and you don't catch it, React automatically drops the optimistic overlay and reverts to the last real state — no manual cleanup needed.

What is the difference between useOptimistic and useState?▼

useState holds authoritative data — it only changes when you explicitly call setState. useOptimistic holds temporary predicted state that auto-reverts. Think of useOptimistic as a 'ghost layer' on top of your real state: it shows what you expect to happen, while the real state catches up asynchronously.

Can I use useOptimistic with React 18?▼

No — useOptimistic is a React 19 API. For React 18, you can implement the same pattern manually: optimistically update a useState variable, run the async call, and revert with the original value in the catch block. It's more boilerplate but the same concept.

What happens if multiple optimistic updates are in flight?▼

React queues and merges multiple optimistic updates. Each addOptimistic call adds to the optimistic layer. As each corresponding async operation completes (or fails), its contribution is committed (or dropped) independently. This lets you handle multiple simultaneous mutations safely.

Is useOptimistic only for lists?▼

No — useOptimistic works for any state shape. It's common with todo lists and forms, but you can use it for counters (optimistic like count), field updates, status toggles, or any UI that should feel instant. The reducer can transform state in any way.

Share this tool: X / Twitter LinkedIn
✓ 60,000+ developers use DevPlaybook ★★★★★ 4.8 / 5 on Gumroad ✓ MIT licensed · Instant download ✓ No login required

Related Products

on Gumroad

You Might Also Need

More Free Tools

Enjoying this tool? Get more like it.

Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.

DevPlaybook Pro 7 days free

Unlock AI-Powered Dev Tools

  • ⚡ AI Code Review, Doc Generator & SQL Builder
  • ⚡ All premium templates & early access
  • ⚡ Member discounts on Gumroad products
Start Free Trial →

$9/mo after trial · Cancel anytime

Want the full toolkit?

Get DevPlaybook Pro

Every template, guide, boilerplate, and automation script in one bundle. 13 premium products — grab them all at once and save big.

$79
$241 separately Save $162 (67% off)
Get DevPlaybook Pro — $79 →

MIT licensed · Instant download · No subscription

See what's included → Browse all deals →