React Query optimistic update for likes

3095
0

Interactive UI should feel instant even when the network isn’t. With optimistic updates, I update the cache immediately and roll back if the server rejects the mutation. The trick is to keep optimistic state small and obvious: update a count and a boolean, not a whole nested object graph. I also cancel any in-flight refetch for the query I’m about to modify; otherwise a slow refetch can overwrite the optimistic state and cause flicker. After the mutation settles, I invalidate the query to reconcile with the server. Done this way, you get a snappy UX while still converging on the backend as the source of truth.