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.
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.
Related Products
on GumroadDevPlaybook Interview Vault
210+ real technical interview questions with model answers, asked at Google, Meta, Amazon, Microsoft, Stripe, and Shopify. Covers Frontend (55), Backend (55), System Design (50), and DSA (50). Available in English and Traditional Chinese.
Buy on Gumroad →TypeScript Quick Reference Toolkit
5 practical reference files for TypeScript developers: cheatsheet, TS vs JS comparison, 20 utility types with real examples, tsconfig best practices template, and 15 common mistakes guide. Print it. Keep it on your desk.
Buy on Gumroad →Developer Interview Vault
210 technical interview questions with model answers, key points, and follow-up questions. Covers Frontend, Backend, System Design, and DSA — bilingual English + Traditional Chinese (ZH-TW). PWYW from $29.
Buy on Gumroad →You Might Also Need
SQL to TypeScript Types
Convert SQL CREATE TABLE statements to TypeScript interfaces and Zod schemas. Maps SQL column types to TypeScript with nullability.
Open free tool →
TanStack Query Config Builder
Configure TanStack Query (React Query v5) staleTime and gcTime visually. Generate QueryClient config and useQuery/useMutation TypeScript code with retry logic.
Open free tool →
TypeScript Discriminated Union Builder
Build TypeScript discriminated unions visually. Add variants with fields and get type-safe switch statements, type guards, and exhaustive checking code.
Open free tool →
More Free Tools
Related Articles
React 19: Complete Guide to New Features in 2026
Everything new in React 19: use() hook, Actions, optimistic updates, ref as prop, form actions, and the React Compiler. Production-ready patterns with TypeScript examples.
React Hooks in 2026: Advanced Patterns and Best Practices
Master React Hooks in 2026 with advanced patterns for state management, side effects, performance optimization, and building custom hooks that scale.
React 19 Complete Guide: useOptimistic, Server Actions & React Compiler 2026
Master React 19's biggest features: useOptimistic hook, Server Actions, React Compiler auto-memoization, the use() hook, and more. Real code examples for every new API.
SolidJS vs React 2026: Performance, DX, and When to Choose Each
Deep comparison of SolidJS and React in 2026: fine-grained reactivity vs virtual DOM, signals vs useState, performance benchmarks, SolidStart vs Next.js, and honest real-world tradeoffs.
Frontend Interview Questions 2026: React, TypeScript & More
The most common frontend interview questions in 2026 with detailed answers covering React, TypeScript, performance, accessibility, and system design.
Enjoying this tool? Get more like it.
Weekly developer tool guides, productivity playbooks & AI tricks. Free. No spam.
Unlock AI-Powered Dev Tools
- ⚡ AI Code Review, Doc Generator & SQL Builder
- ⚡ All premium templates & early access
- ⚡ Member discounts on Gumroad products
$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.
MIT licensed · Instant download · No subscription
See what's included → Browse all deals →