Skip to content

FlashcardNot attempted

Optimistic updates

Recall it before you reveal

How do you implement an optimistic "like" button safely?

Space to reveal
Show answer & explanation

1. Update the UI immediately (increment the count, mark as liked) and remember the previous state. 2. Send the request in the background. 3. On success, optionally reconcile with the server's response. 4. On failure, roll back to the previous state and show an error. 5. Guard against races: disable repeat clicks or send the final intended state, and dedupe requests.

React 19's useOptimistic and React Query's onMutate/onError encode this pattern.