English for TanStack Query

Learn the English vocabulary for discussing TanStack Query (React Query), including caching, stale time, query invalidation, and background refetching.

TanStack Query solves a problem most teams underestimate — server state isn’t the same as client state — and its vocabulary is precise about caching behavior in a way that’s easy to misuse if you’re not clear on the terms.

Key Vocabulary

Server state — data that lives on the server and is fetched asynchronously, as distinct from client state like form inputs or UI toggles, a distinction TanStack Query is built specifically to manage. “We were trying to manage this with useState and manual refetching, but this is server state, not client state — that’s exactly the problem TanStack Query solves, with caching and invalidation built in.”

Stale time — how long fetched data is considered fresh before TanStack Query will refetch it in the background on the next relevant trigger, configurable per query and central to controlling refetch frequency. “We set the stale time to five minutes on this query because the data barely changes — without it, the default meant it was refetching every time the component remounted, which was unnecessary load.”

Query invalidation — explicitly marking cached data as stale, typically after a mutation, so TanStack Query refetches it, which is how the cache stays in sync with server changes caused by the app itself. “After this mutation succeeds, we call query invalidation on the affected query key — that tells TanStack Query the cached list is now stale, so it refetches automatically instead of showing outdated data.”

Query key — the array or string TanStack Query uses to uniquely identify and cache a specific query, meaning two calls with the same key share a cache entry, and invalidation targets specific keys. “These two components are unexpectedly sharing loading state because they use the same query key — if you want them cached independently, they need different keys, even though they’re fetching similar data.”

Background refetching — TanStack Query automatically refetching stale data behind the scenes, such as on window refocus or reconnect, updating the UI once new data arrives without an explicit loading state blocking the user. “The data updates automatically when you switch back to this tab because of background refetching — TanStack Query refetches on window focus by default, and swaps in fresh data without showing a loading spinner over stale content.”

Common Phrases

  • “Is this server state, or should it actually be local component state?”
  • “What stale time makes sense for how often this data actually changes?”
  • “Do we need to trigger query invalidation after this mutation?”
  • “Are these using the same query key, or should they be cached separately?”
  • “Is background refetching causing this unexpected extra request?”

Example Sentences

Diagnosing a stale data bug: “Users are seeing outdated data after they submit this form because we’re not triggering query invalidation on the related query key — the mutation succeeds, but nothing tells TanStack Query the cached list needs refetching.”

Explaining a caching decision: “We set a longer stale time on this query specifically because the underlying data changes maybe once a day — treating it as fresh for an hour avoids unnecessary refetches without risking meaningfully outdated data.”

Debugging a shared-cache issue: “These two widgets were flickering together because they share a query key even though they show slightly different filtered views — giving them distinct keys let TanStack Query cache and refetch them independently.”

Professional Tips

  • Use TanStack Query specifically for server state, and keep genuinely local UI state, like form inputs, in regular component state instead — mixing the two into one system usually backfires.
  • Tune stale time deliberately per query based on how often the underlying data actually changes — the default of zero refetches aggressively and isn’t right for slow-changing data.
  • Always pair mutations with explicit query invalidation for affected queries — forgetting this is the most common cause of a UI showing stale data after a successful write.
  • Design query keys carefully, including all the parameters that affect the result — an incomplete key causes unrelated queries to incorrectly share a cache entry.
  • Understand background refetching triggers, like window refocus, before debugging “mystery” network requests — they’re often intentional, not a bug.

Practice Exercise

  1. Explain the difference between server state and client state with an example.
  2. Describe why query invalidation is necessary after a mutation.
  3. Write a sentence explaining how an incomplete query key can cause a caching bug.