Next.js Optimistic UI Bug: Rapid Clicks Can Silently Desync Your Database

A developer building a Next.js 16 task list with optimistic UI discovered a subtle but serious bug: clicking a checkbox five times rapidly sent overlapping network requests that corrupted the database state without triggering any visible error. The UI appeared correct on screen while the server held a different value, a silent mismatch that is especially easy to miss because optimistic UI is designed to look right instantly. The fix involved tracking a pending request per list item using a state variable and disabling further clicks on that row until the in-flight request settled, ensuring only one request fires per item at a time. The developer also clarified a common misconception about rollbacks: for toggle actions, React's useOptimistic automatically reverts to the real state once a transition fails, so a manual undo dispatch is unnecessary. The one exception is newly created items with client-only placeholder IDs, which must be manually removed in a catch block since no prior server state exists to fall back to.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in