Optimistic updates
Use useOptimistic to show a Server Action's result immediately, roll back on failure, and know when waiting for the server is the better choice.
Optimistic updates
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- An optimistic update changes the UI as if the server had already succeeded, then reconciles with the real result when it arrives.
- useOptimistic takes the real state and an update function, and returns the optimistic state plus a function that applies an update.
- The optimistic value lasts only while the action runs; when the real data arrives, or the action fails, it reverts to whatever the real state is.
- Use it for low-risk, usually-successful changes such as likes, toggles, and chat messages; avoid it where a failure would be confusing or costly, such as payments.
- The Server Action still validates and mutates for real; the optimistic copy is only presentation.
The latency you can hide
A like button that waits for the server feels sluggish, even if the server is fast. The user clicked; the world should change. An optimistic update gives them that: show the new state immediately, then confirm with the server.
The word “optimistic” is literal. You are betting the request will succeed. Most of the time it does. When it does not, you undo the change and say so.
useOptimistic
React ships a hook for this. It takes the real state and a function describing how an update changes it, and returns the optimistic state and a way to trigger the update.
'use client'
import { useOptimistic } from 'react'
import { send } from './actions'
type Message = { message: string }
export function Thread({ messages }: { messages: Message[] }) {
const [optimisticMessages, addOptimisticMessage] = useOptimistic<Message[], string>(
messages,
(state, newMessage) => [...state, { message: newMessage }],
)
const formAction = async (formData: FormData) => {
const message = formData.get('message') as string
addOptimisticMessage(message)
await send(message)
}
return (
<div>
{optimisticMessages.map((m, i) => (
<div key={i}>{m.message}</div>
))}
<form action={formAction}>
<input type="text" name="message" />
<button type="submit">Send</button>
</form>
</div>
)
}
This is the example from the Next.js forms guide. The sequence is:
- The user submits.
addOptimisticMessageruns first, so the list shows the new message at once. send(message)is a Server Action. It runs on the server.- When the action finishes and the page re-renders with fresh
messages, the optimistic state is replaced by the real list.
The real messages prop is the source of truth. The optimistic version is a temporary overlay that lasts only while the action is running. If send throws, the overlay disappears and the list shows what the server has, which is why a failure needs a visible message; otherwise the item vanishes without explanation.
Make the server action refresh the real data
For step 3 to work, the action must cause the real data to re-render. Use the invalidation from revalidation and cache tags: updateTag('messages') inside the Server Action, so the same response that finishes the action brings the new list. If the action invalidates nothing, the real data never changes and the optimistic message vanishes when the action completes.
'use server'
import { updateTag } from 'next/cache'
export async function send(message: string) {
await db.message.create({ data: { text: message } })
updateTag('messages')
}
When to be optimistic
Good fits are low-risk, almost-always-successful interactions: likes, toggles, reorders, adding a comment or chat message.
Poor fits are anything where “success” shown and then taken back would hurt: payments, destructive deletes, actions that depend on server-side rules the client cannot predict. For those, show a pending state and wait.
Two practical cautions. Give optimistic items a temporary id or key so they do not collide with real ones, and design the failure message before you ship the happy path.
Related pieces
useOptimistic runs inside a transition or action. That is why it works in a form action as above. The hook itself is React’s; useTransition and useDeferredValue explains the machinery, and server actions and mutations covers the React view of the pattern. If you use a client library cache like SWR or TanStack Query, the docs describe updating the browser cache immediately and restoring the previous value if the write fails; that is the same idea one layer over.
What the video covers
Tobi Mey’s January 2026 video covers, per its chapters: the problem with loading states, understanding the UI, implementing useOptimistic, handling errors and rollbacks, more advanced CRUD operations, and when to avoid optimistic UI. There is a sponsor segment about 45 seconds in. It is short and recent from a smaller channel, which is why the more detailed Codevolution runner-up, from the Next.js 15 series, is linked as a second look.
Where this fits
You now have the full client-side loop for mutations through actions. Not every write comes from your own UI, though; other systems need an HTTP endpoint. Next: route handlers.
Check yourself
What is the difference between an optimistic update and a pending state?
A pending state tells the user the request is in flight and shows the old data, while an optimistic update shows the expected new data right away and corrects it if the request fails.
What happens to a useOptimistic value when its Server Action throws?
The optimistic value reverts to the real state, because it only lasts while the action runs, so the added item disappears and you should tell the user it failed.
What two arguments does useOptimistic take?
The real state, which it reverts to, and a reducer-like function that receives the current state and an update and returns the optimistic state.
Why is optimistic UI a poor choice for a payment button?
A failure after showing success would be confusing and costly, so the UI should wait for the server's answer.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.