A successful mutation does not automatically update every cached view that depends on it. With TanStack Query, map the changed record to the queries that display or derive from it, then update or invalidate those keys at the correct point in the mutation lifecycle. Also verify that the active screen actually uses the key being targeted.
Trace the write and the dependent views
Imagine changing a daily activity record. The activity list, daily summary and nutrition view may depend on that record, but the account settings screen may not. Write down this dependency map before adding a global invalidation. It makes a stale-screen bug easier to reproduce and avoids refetching unrelated data on every save.
| View | Illustrative query key | Expected response to a confirmed update |
|---|---|---|
| Daily activity | metric-logs / account / day | Refresh the affected day |
| Dashboard total | today-summary / account / day | Recompute or refetch its derived values |
| Nutrition allowance | nutrition-summary / account / day | Refresh only if the changed metric affects it |
| Account preferences | preferences / account | Remain untouched when unrelated |
An UltraFit360 source example
The reviewed health-sync service invalidates day-and-user-scoped metric logs, today’s summary, watch calories and nutrition-summary keys. The app-launch hook separately seeds several caches from a launch response. That is useful evidence of explicit cache ownership. It is not evidence that every state is synchronized in realtime or that cross-device latency meets a measured target.
There is an important ordering detail: the health service can save local data and invalidate views before attempting a backend sync, and a backend failure retains the local data. A local “saved” state therefore must not be treated as proof that another device or coach can already read the same value. Design the UI and tests around those distinct states.
Choose invalidation, a cache update or optimism deliberately
Invalidation marks matching queries stale; active queries can then refetch under TanStack Query’s behavior and configured options. If a mutation returns the authoritative updated object, a targeted cache update can avoid waiting for an extra read. An optimistic update needs a rollback or reconciliation path when the request fails. Await the work required by your product’s completion state rather than treating every approach as interchangeable.
TanStack Query: invalidation after mutations
TanStack Query: query invalidation and matching
Debug a stale screen in this order
- Inspect the exact key used by the screen, including account, date, filters and value types.
- Confirm the mutation actually succeeded and identify whether the visible value is local or server-backed.
- Check whether invalidation matches that key and whether the query is active or disabled.
- Inspect the refetch response before blaming the renderer; the server may still return an older value.
- Repeat with a failed request, account switch and another device updating the record.
Account-switching cache isolation
Launch-data hydration and startup caches
Does invalidateQueries instantly replace cached data?
It marks matching data stale and can trigger refetches according to query state and options. It does not itself supply the new server value.
Should every mutation invalidate every query?
Start with the dependent views. Broad invalidation can be appropriate for a bounded application, but it should be an intentional tradeoff rather than a substitute for understanding cache ownership.
