useOptimistic
The React hook that shows a temporary optimistic value while an Action is pending, then returns to the real state when it settles.
In more detail
useOptimistic(state, updateFn) returns the value to display and a function to apply an optimistic change; call that function inside an Action or transition. If the Action fails, the optimistic value is simply dropped, so no manual rollback is needed.
Examples
const [likes, addLike] = useOptimistic(count, (c, n: number) => c + n);const [optimisticLikes, addLike] = useOptimistic(likes, (cur, n) => cur + n);Related terms
- optimistic UI — Updating the screen immediately as if an action succeeded, before the server confirms it, and undoing the change if it fails.
- startTransition — A React function that marks state updates, including async actions, as a transition that can run without blocking the interface.
- rollback — Returning a system to a previous known-good version after a bad change.
- 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.
- 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.
- transition — A React state update marked as not urgent, which React may interrupt or delay so urgent input stays responsive.
Taught in
- Action State: useActionState & Optimistic Form UI — Next.js 15 & Modern App Router Architecture
- Optimistic UI: useOptimistic Rollbacks & Transitions — Next.js 15 & Modern App Router Architecture
- useActionState: Async Action Handlers And Pending States — React 19 & Modern Component Architecture
- useOptimistic: Instantaneous UI Updates with Rollback — React 19 & Modern Component Architecture