Question presented to candidate:
"A like button should feel instant even though the request takes 300ms. How does useOptimistic help, and what happens if the request fails?"
What a strong answer should cover:
useOptimistic(actualState, updateFn)returns a value that may differ from real state while an action is pending, plus a function to apply an optimistic update.- It shows a predicted result immediately, then automatically reverts to the real state when the transition settles.
- Error handling is the headline benefit: if the action throws, React discards the optimistic value automatically. No manual rollback code.
- It must be used inside a transition — an action, or
startTransition. Outside one, the optimistic value is discarded immediately. - The update function is a reducer:
(currentState, optimisticValue) => newState, so several optimistic updates compose. - Pairs naturally with form actions and
useActionState. - The distinction from plain local state: hand-rolled optimistic UI means writing the revert path yourself, and getting it wrong on error or on rapid repeated actions.
- Judgement: optimistic UI suits high-success, low-stakes actions — likes, toggles, reordering. Not payments.
Clarifying questions expected:
- "How likely is this action to fail, and how bad is a visible revert?" — that decides whether optimistic UI is appropriate at all.
- "Are we already using form actions?"
Code / implementation expected: Yes — an optimistic list or counter, including a failure path.