Server State Is Not App State
What changed once I stopped putting API data in a global store and let TanStack Query own it. Keys, invalidation, and the two settings that matter most.
For a long time, my React apps fetched data in an effect and put the result in a global store. Then every screen needed loading flags, error flags, a refetch function, and some way to know when the data was stale. The store grew into a hand-written cache, and not a good one.
The shift that fixed it is mostly a change of vocabulary. There are two kinds of state:
- App state belongs to the browser: which tab is open, what's typed in a form, whether a modal is showing.
- Server state belongs to the server. Your copy is a snapshot that starts going stale the moment it arrives.
TanStack Query is a tool for the second kind. Once I stopped treating it as "a fetching library," the patterns made sense.
Make query keys a first-class thing
Keys are how the cache knows what's what. Scatter string arrays around and invalidation becomes guesswork. Put them in one place:
export const orderKeys = {
all: ["orders"] as const,
lists: () => [...orderKeys.all, "list"] as const,
list: (filters: OrderFilters) => [...orderKeys.lists(), filters] as const,
detail: (id: string) => [...orderKeys.all, "detail", id] as const,
};Because keys are hierarchical, invalidating orderKeys.all refreshes every list and every detail at once. orderKeys.lists() refreshes every filtered list, and orderKeys.detail(id) touches just one.
staleTime is the setting that matters
The default staleTime is zero: every mount and window focus can trigger a refetch. That's a safe default and a noisy one. Set it per kind of data:
useQuery({
queryKey: orderKeys.list(filters),
queryFn: () => api.orders(filters),
staleTime: 30_000, // orders change often, but not every second
});Reference data like regions or categories can be stale for an hour. A live order queue maybe ten seconds. Thinking about this per query is most of the design work.
Invalidate after mutations, don't patch by hand
After a mutation, the lazy correct move is to tell the cache what's now out of date:
const approve = useMutation({
mutationFn: api.approveOrder,
onSuccess: (_, id) => {
queryClient.invalidateQueries({ queryKey: orderKeys.detail(id) });
queryClient.invalidateQueries({ queryKey: orderKeys.lists() });
},
});Hand-editing cached data is tempting and fragile. Save optimistic updates for the few interactions where the wait is actually noticeable, like toggles and reordering.
Keep the old page while the next one loads
Paginated tables flash empty on every page change unless you ask otherwise. In v5:
import { keepPreviousData } from "@tanstack/react-query";
useQuery({
queryKey: orderKeys.list({ page }),
queryFn: () => api.orders({ page }),
placeholderData: keepPreviousData,
});The table keeps showing page 2 until page 3 arrives. A small thing users notice immediately.
What's left in the global store
After moving server state out, our global store shrank to a handful of values: the current user's preferences, a sidebar toggle, a draft form. Small enough that I started questioning whether it needed a library at all. That's usually a good sign.
Keep reading
All posts2025
3 min
React Compiler 1.0 and the useMemo I Got to Delete
The compiler memoizes for you at build time. What to remove, what to keep, and the rule it quietly enforces.
reactfrontend3 min
React 19 Actions Replaced My Form Boilerplate
Pending flags, error state, optimistic rows, and a reset after submit. I used to write all of it by hand for every form. React 19 turned most of it into three hooks and a function passed to the form.
reactfrontend