0
0
NextJSframework~10 mins

Optimistic updates pattern in NextJS - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Optimistic updates pattern
User triggers update
Update UI immediately
Send update request to server
Server responds
Keep UI
This flow shows how the UI updates right away when the user acts, then waits for the server response to confirm or undo the change.
Execution Sample
NextJS
const [items, setItems] = useState(['apple', 'banana']);

async function addItem(newItem) {
  const oldItems = items;
  setItems([...items, newItem]);
  try {
    await fetch('/api/add', { method: 'POST', body: JSON.stringify({ item: newItem }) });
  } catch {
    setItems(oldItems); // revert on failure
  }
}
This code adds an item to the list immediately, then tries to save it on the server. If saving fails, it reverts the list.
Execution Table
StepActionUI State (items)Server RequestServer ResponseResulting UI State
1User clicks add 'orange'['apple', 'banana']NoNo['apple', 'banana']
2UI updates immediately['apple', 'banana', 'orange']NoNo['apple', 'banana', 'orange']
3Send POST request to /api/add['apple', 'banana', 'orange']POST /api/add {item: 'orange'}Pending['apple', 'banana', 'orange']
4Server responds success['apple', 'banana', 'orange']POST /api/add {item: 'orange'}Success['apple', 'banana', 'orange']
5No UI change needed['apple', 'banana', 'orange']NoNo['apple', 'banana', 'orange']
6User clicks add 'grape'['apple', 'banana', 'orange']NoNo['apple', 'banana', 'orange']
7UI updates immediately['apple', 'banana', 'orange', 'grape']NoNo['apple', 'banana', 'orange', 'grape']
8Send POST request to /api/add['apple', 'banana', 'orange', 'grape']POST /api/add {item: 'grape'}Pending['apple', 'banana', 'orange', 'grape']
9Server responds failure['apple', 'banana', 'orange', 'grape']POST /api/add {item: 'grape'}Failure['apple', 'banana', 'orange']
10UI reverts to old state['apple', 'banana', 'orange']NoNo['apple', 'banana', 'orange']
💡 Execution stops after server response and UI state is confirmed or reverted.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 9Final
items['apple', 'banana']['apple', 'banana', 'orange']['apple', 'banana', 'orange']['apple', 'banana', 'orange', 'grape']['apple', 'banana', 'orange']['apple', 'banana', 'orange']
Key Moments - 3 Insights
Why does the UI update before the server confirms the change?
The UI updates immediately to give instant feedback to the user, as shown in steps 2 and 7 of the execution_table. This is the core of optimistic updates.
What happens if the server rejects the update?
If the server responds with failure (step 9), the UI reverts to the previous state (step 10), undoing the optimistic change.
Why do we keep a copy of the old state?
We keep the old state to revert the UI if the server rejects the update, as seen in the catch block in the code and steps 9-10 in the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the UI state after the server responds successfully?
A['apple', 'banana', 'orange']
B['apple', 'banana']
C['apple', 'banana', 'orange', 'grape']
D[]
💡 Hint
Check the 'Resulting UI State' column at step 4 in the execution_table.
At which step does the UI revert to the old state after a failed server response?
AStep 9
BStep 10
CStep 7
DStep 5
💡 Hint
Look for the step where the 'Resulting UI State' changes back to the previous list in the execution_table.
If the server always succeeds, how would the variable_tracker change?
AThe 'items' variable would revert after each update.
BThe 'items' variable would stay the same as start.
CThe 'items' variable would never revert and keep growing.
DThe 'items' variable would be empty.
💡 Hint
Check how 'items' changes after successful and failed responses in variable_tracker.
Concept Snapshot
Optimistic updates pattern:
- Update UI immediately on user action
- Send server request in background
- If server succeeds, keep UI as is
- If server fails, revert UI to old state
- Improves user experience by reducing wait time
Full Transcript
The optimistic updates pattern lets the UI change right away when a user acts, without waiting for the server. This makes the app feel faster and more responsive. The code first updates the UI state, then sends a request to the server. If the server confirms success, the UI stays the same. If the server fails, the UI reverts to the previous state. This pattern requires keeping a copy of the old state to revert if needed. The execution table shows each step: user action, UI update, server request, server response, and final UI state. The variable tracker follows the list of items as it changes. Key moments include understanding why the UI updates before server confirmation and how failures are handled. The visual quiz tests understanding of these steps. This pattern is common in Next.js apps to improve user experience.