Skip to content
public-library›Rules›TanStack Query›Make optimistic updates reversible, and decide them once

Make optimistic updates reversible, and decide them once

When a mutation updates the UI before the server responds, cancel in-flight queries for the affected keys, snapshot their data, apply the update, roll back on error, and invalidate when the mutation settles. Decide in onMutate whether the optimistic change applies, and read that decision in later callbacks instead of recomputing it.

In the full rule