advanced
Optimistic updates
Show expected success immediately while planning rollback, conflict handling, and user feedback.
Optimistic updates show the expected result before the server confirms, improving perceived speed. On failure, roll back cache and state and inform the user. Conflict resolution matters for concurrent edits — last-write-wins is not always acceptable.
Libraries expose `onMutate`, snapshot previous data, and restore on error. Keep optimistic data distinguishable if partial success is possible. Do not optimistically delete without undo affordance.
On interviews, explain the concept with a concrete example and name the behavior interviewers probe.
Common pitfalls include hiding data flow, over-splitting components, and putting side effects in render.
The trade-off is often clarity versus reuse or explicit props versus convenience.
Checklist:
- Snapshot before optimistic write.
- Roll back on error with messaging.
- Define conflict policy for shared records.