optimistic update
Showing the result of a user’s action on screen immediately, before the server confirms it, and undoing it if the server says no.
In more detail
The TanStack Query recipe has four steps: cancel in-flight queries for that data, snapshot the current cache, write the optimistic change, and on error restore the snapshot. Finally, invalidate the query when the mutation settles so the screen matches the server.
Example
In React 19, useOptimistic keeps the optimistic value only while the Action is pending and falls back to the real state when it finishes or fails.Related terms
- mutation lifecycle callbacks — The functions TanStack Query calls at each stage of a mutation: onMutate before it is sent, onSuccess or onError after, and onSettled at the very end.
- snapshot — A saved copy of some state at one moment, kept so it can be restored later.
- rollback — Returning a system to a previous known-good version after a bad change.
- TanStack Query — A library that fetches, caches and refreshes server data in frontend apps, and runs changes to it as mutations.
- useOptimistic — The React hook that shows a temporary optimistic value while an Action is pending, then returns to the real state when it settles.
- Action (React) — In React 19, an async function run inside a transition, typically to submit data, with pending state, errors and optimistic updates handled by React.
Taught in
- Optimistic UI Architecture: Cache & Rollback Protocols — Production Full-Stack Engineering
- useActionState: Async Action Handlers And Pending States — React 19 & Modern Component Architecture
- useOptimistic: Instantaneous UI Updates with Rollback — React 19 & Modern Component Architecture