When to use
✅ Toggling a state
Mark task done; like a post; star a comment.
✅ Adding to a list
Send a chat message; add a todo.
❌ Stateful side effects
Charging a credit card. Don’t predict success — wait.
❌ Operations that often fail
Form submissions with strict validation. Failure recovery is more painful than the wait.
Pattern with Convex
Convex’suseMutation doesn’t have optimistic updates built in. Use Zustand or component state for the optimistic layer:
tasks re-fetches via reactive sync; the optimistic flag is dropped, and the displayed value is now the real one.
Reconciliation on failure
If the mutation fails, the optimistic toggle should revert:Tips
Related
State management
Where the optimistic state lives.
Convex mutations
The underlying writes.
Real-time sync
What reconciles after.
