Challenge - 5 Problems
Optimistic Updates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What happens immediately after triggering an optimistic update?
Consider a Next.js component using optimistic updates to add a new item to a list. What will the user see right after clicking the add button, before the server confirms?
NextJS
const [items, setItems] = useState(['apple', 'banana']); async function addItem(newItem) { setItems([...items, newItem]); // optimistic update await fetch('/api/add', { method: 'POST', body: JSON.stringify({ item: newItem }) }); // no rollback on failure here } // User clicks button to add 'orange'
Attempts:
2 left
💡 Hint
Think about what optimistic update means: showing changes before confirmation.
✗ Incorrect
Optimistic updates show the new data immediately to improve user experience. The UI updates before the server confirms the change.
❓ state_output
intermediate2:00remaining
What is the state of the list after a failed optimistic update?
Given this Next.js code snippet using optimistic updates, what will be the final state of the items if the server request fails and no rollback is implemented?
NextJS
const [items, setItems] = useState(['cat', 'dog']); async function addPet(pet) { setItems([...items, pet]); // optimistic update try { const res = await fetch('/api/addPet', { method: 'POST', body: JSON.stringify({ pet }) }); if (!res.ok) throw new Error('Failed'); } catch { // no rollback } } // addPet('rabbit') is called but server returns error
Attempts:
2 left
💡 Hint
No rollback means the optimistic change stays even if server fails.
✗ Incorrect
Without rollback, the UI keeps the optimistic change even if the server rejects it, so 'rabbit' stays in the list.
🔧 Debug
advanced2:00remaining
Why does this optimistic update cause a UI flicker?
Examine this Next.js code using optimistic updates. Why does the UI flicker when adding a new item?
NextJS
const [items, setItems] = useState([]); async function addItem(item) { setItems([]); // clear list first setItems([...items, item]); // optimistic update await fetch('/api/add', { method: 'POST', body: JSON.stringify({ item }) }); } // User adds 'pear'
Attempts:
2 left
💡 Hint
Think about what happens when you clear the list before adding.
✗ Incorrect
Clearing the list causes the UI to render empty briefly, then re-render with the new item, causing flicker.
📝 Syntax
advanced2:00remaining
Which option correctly implements rollback on optimistic update failure?
You want to optimistically add an item but revert if the server fails. Which code snippet correctly implements rollback?
Attempts:
2 left
💡 Hint
Rollback means restoring previous state only if fetch fails.
✗ Incorrect
Option B saves old state, updates optimistically, and restores old state only on error.
Other options either restore state unconditionally or clear incorrectly.
🧠 Conceptual
expert2:00remaining
Why is optimistic update pattern beneficial in Next.js apps?
Select the best explanation for why optimistic updates improve user experience in Next.js applications.
Attempts:
2 left
💡 Hint
Think about user perception of speed and responsiveness.
✗ Incorrect
Optimistic updates improve perceived speed by updating UI immediately, making apps feel more responsive.
They do not guarantee consistency or reduce server load directly.