0
0
NextJSframework~20 mins

Optimistic updates pattern in NextJS - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Optimistic Updates Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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'
AThe list immediately shows 'orange' added, even before server response.
BThe list removes all items temporarily during the request.
CThe list waits until server confirms before showing 'orange'.
DThe list shows an error message immediately after clicking.
Attempts:
2 left
💡 Hint
Think about what optimistic update means: showing changes before confirmation.
state_output
intermediate
2: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
AThe list becomes empty due to error.
B['cat', 'dog'] remains because rollback removes 'rabbit'.
C['cat', 'dog', 'rabbit'] remains in the list.
DThe list shows only 'rabbit'.
Attempts:
2 left
💡 Hint
No rollback means the optimistic change stays even if server fails.
🔧 Debug
advanced
2: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'
ABecause setItems is called twice in a row, which is invalid syntax.
BBecause the fetch call is blocking the UI thread.
CBecause the optimistic update is missing a rollback.
DBecause the list is cleared before adding, causing a brief empty render.
Attempts:
2 left
💡 Hint
Think about what happens when you clear the list before adding.
📝 Syntax
advanced
2: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?
A
setItems([...items, newItem]);
await fetch('/api/add', { method: 'POST', body: JSON.stringify({ newItem }) });
setItems(items);
B
const oldItems = [...items];
setItems([...items, newItem]);
try {
  await fetch('/api/add', { method: 'POST', body: JSON.stringify({ newItem }) });
} catch {
  setItems(oldItems);
}
C
setItems([...items, newItem]);
try {
  await fetch('/api/add', { method: 'POST', body: JSON.stringify({ newItem }) });
} catch {
  setItems([]);
}
D
const oldItems = items;
setItems([...items, newItem]);
await fetch('/api/add', { method: 'POST', body: JSON.stringify({ newItem }) });
setItems(oldItems);
Attempts:
2 left
💡 Hint
Rollback means restoring previous state only if fetch fails.
🧠 Conceptual
expert
2: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.
AThey make the UI feel faster by showing changes immediately without waiting for server response.
BThey guarantee data consistency by waiting for server confirmation before updating UI.
CThey reduce server load by batching multiple requests into one.
DThey prevent any errors by disabling user input during server requests.
Attempts:
2 left
💡 Hint
Think about user perception of speed and responsiveness.