0
0
NextJSframework~10 mins

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

Choose your learning style9 modes available
Concept Flow - Optimistic state updates
User triggers update
Update UI state immediately
Send update request to server
Server responds
Keep UI
The UI updates right away when the user acts, then waits for the server. If the server says no, the UI goes back.
Execution Sample
NextJS
const [count, setCount] = useState(0);

async function increment() {
  setCount(prevCount => prevCount + 1); // optimistic update
  const res = await fetch('/api/increment', { method: 'POST' });
  if (!res.ok) setCount(prevCount => prevCount - 1); // rollback on failure
}
This code increases a counter on the screen immediately, then tries to update the server. If the server fails, it resets the counter.
Execution Table
StepActionUI State (count)Server ResponseResulting UI State
1User clicks increment0N/A0
2Optimistic update: setCount(count + 1)1Waiting1
3Send POST to /api/increment1Waiting1
4Server responds OK1Success1
5No rollback needed1Success1
6User clicks increment again1N/A1
7Optimistic update: setCount(count + 1)2Waiting2
8Send POST to /api/increment2Waiting2
9Server responds ERROR2Failure2
10Rollback: setCount(previous count)1Failure1
💡 Execution stops after rollback or confirmation from server.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 9Final
count011221
Key Moments - 3 Insights
Why does the UI update before the server confirms?
The UI updates immediately to keep the app feeling fast and responsive, as shown in step 2 and 7 of the execution_table.
What happens if the server rejects the update?
The UI rolls back to the previous state to stay correct, as seen in step 10 where count goes back from 2 to 1.
Why do we keep track of the old state?
We save the old state so we can restore it if the server rejects the change, demonstrated by resetting count in step 10.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the UI state (count) immediately after the optimistic update in step 2?
A1
B0
C2
DUndefined
💡 Hint
Check the 'UI State (count)' column at step 2 in the execution_table.
At which step does the UI rollback happen due to server failure?
AStep 4
BStep 10
CStep 9
DStep 7
💡 Hint
Look for the step where 'Rollback' is mentioned in the 'Action' column.
If the server always responds successfully, how would the final UI state after two increments change?
AIt would rollback to 0
BIt would stay at 1
CIt would be 2
DIt would be undefined
💡 Hint
Refer to the 'Resulting UI State' column after successful server responses in the execution_table.
Concept Snapshot
Optimistic state updates:
- Update UI immediately on user action
- Send server request in background
- If server confirms, keep UI
- If server rejects, rollback UI
- Makes app feel fast and responsive
Full Transcript
Optimistic state updates mean the app changes what you see right away when you do something, like clicking a button. Then it asks the server to save that change. If the server says yes, the app keeps the change. If the server says no, the app goes back to how it was before. This way, the app feels quick and smooth. In the example, when you click increment, the number on screen goes up immediately. Then the app sends a request to the server. If the server fails, the number goes back down. This helps users feel the app is fast even if the server is slow.