Optimistic updates
What you need to know
Optimistic UI shows the result a user expects while a request is still in flight. It improves perceived responsiveness but the authoritative value remains the confirmed server result.
By the end, you can: predict rollback after rejection; reconcile with server state.
Optimistic title preview with explicit rejection
'use client';import { useOptimistic, useState } from 'react';export default function Preview() { const [confirmed, setConfirmed] = useState('Fern'); const [error, setError] = useState(''); const [title, setTitle] = useOptimistic(confirmed); async function change(data: FormData) { const next = String(data.get('title') ?? '').trim(); setError(''); setTitle(next); await new Promise(resolve => setTimeout(resolve, 300)); if (next.length < 3) { setError('Title rejected'); return; } setConfirmed(next); } return <form action={change}> <label htmlFor="rename">New title</label><input id="rename" name="title" /> <button>Preview change</button><p>Title: {title}</p> <p role="status">{error}</p> </form>;}Expected behavior: A valid title becomes confirmed; an invalid title disappears from the optimistic display after the action completes. This is a local simulation, not a save operation.
How it works
React’s useOptimistic provides a temporary view derived from a confirmed value and an optional pure reducer. Apply an optimistic change during an Action or Transition, not during render. When the operation finishes, the view returns to its confirmed base; on success that base must be updated, usually by a returned result or server revalidation. On failure retain the last confirmed state and explain the error. A reducer must not write to a database or mutate the input. Consider a reading list: optimistic removal is reasonable if reversible, but claiming a payment succeeded before confirmation is not. Concurrent edits may be rejected using a stored revision; show the current server version and offer a deliberate retry. This standalone simulation deliberately rejects one title, and makes no network requests. It does not prove that overlapping submissions resolve in the intended order. Use pending feedback, serialize or explicitly reconcile edits as appropriate, and have the server reject stale revisions; a disabled button alone does not provide concurrency control.
Pure optimistic reducer
type Book = {id: string; title: string};function rename(books: Book[], change: Book): Book[] { return books.map(book => book.id === change.id ? {...book, title: change.title} : book);}const books = [{id:'b1',title:'Fern'}, {id:'b2',title:'Moss'}];console.log(rename(books, {id:'b1',title:'Oak'})[0].title);console.log(books[0].title);Expected behavior: Oak, then Fern: the original list remains unchanged.
Common mistake
Updating confirmed state before the server confirms the operation removes the reliable rollback target. Keep the two concepts separate. Never swallow a rejected save behind a success label.
Practice and verification
Predict the screen for a rejected rename, then for a successful rename. In a real app fixture replace the simulated delay with an authorized action, verify revalidation, and test conflict handling with two independent clients.
Scope: App Router, proposed Next.js 16.x, React 19 family, Cache Components disabled. Framework snippets are static or explicitly partial; actual browser, auth and generated-route checks are pending. SQLite exercises run separately in the authoring environment, not the ByteMind evaluator.
Further reading: Optimistic · Forms · Mutating