TanStack Query Invalidation
Use TanStack Query invalidation to refetch only the queries a mutation touches, with query keys as arrays and partial invalidation by prefix.
On this page
Introduction
This guide covers TanStack Query invalidation after writes, a recurring concern in the data-fetching scenario. After a write, you need to refetch the queries that depend on the changed data, but not every query on the page. TanStack Query treats keys as arrays, so invalidating a prefix refetches every query under it, which is the granularity that matches a real mutation.
Keys as arrays
Use an array with a stable prefix and the variables after, so a prefix invalidation refetches the whole family and a full key targets one query.
The trade-off worth naming is that use an array with a stable prefix and the variables after, so a prefix invalidation refetches the whole family and a full key targets one query adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
useQuery({ queryKey: ['orders', page, filter], queryFn: () => fetchOrders(page, filter) });
Invalidate by prefix
After a write, invalidate the prefix so every query under it refetches, which refetches the family without you naming each member.
The trade-off worth naming is that after a write, invalidate the prefix so every query under it refetches, which refetches the family without you naming each member adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
const m = useMutation({
mutationFn: addOrder,
onSuccess: () => queryClient.invalidateQueries({ queryKey: ['orders'] }),
});
Optimistic patch
When you know the new shape, setQueryData to patch the cache and avoid a round trip, rolling back if the mutation fails, which is optimistic update.
The trade-off worth naming is that when you know the new shape, setquerydata to patch the cache and avoid a round trip, rolling back if the mutation fails, which is optimistic update adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Partial invalidation
Invalidate a deeper prefix to refetch only a slice, so a comment write does not refetch the whole post list, which keeps the refetch set tight.
The trade-off worth naming is that invalidate a deeper prefix to refetch only a slice, so a comment write does not refetch the whole post list, which keeps the refetch set tight adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Predicate invalidation
Use the predicate option to invalidate queries matching a condition, which is the escape hatch when a prefix is not expressive enough.
The trade-off worth naming is that use the predicate option to invalidate queries matching a condition, which is the escape hatch when a prefix is not expressive enough adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Test the refetch set
Trigger the mutation and assert the matching queries refetch and unrelated ones do not, so invalidation is precise.
The trade-off worth naming is that trigger the mutation and assert the matching queries refetch and unrelated ones do not, so invalidation is precise adds a measurable step to the hot path, so measure it before assuming it is free. A reasonable approach is to validate once at the edge, fail closed with a structured error, and log the rejection reason so a spike in bad input is visible on a dashboard rather than silent. Pair the implementation with a test that asserts a valid payload passes and at least one representative violation is rejected with the expected message; this is the cheapest way to keep the guarantee honest as the schema evolves.
Sources
Sources: TanStack Query, Query keys.
FAQ
- Why arrays for query keys?
- So you can invalidate by prefix; invalidateQueries(["orders"]) refetches every query whose key starts with orders, which is how you refetch a family without naming each one.
- invalidateQueries vs setQueryData?
- Invalidate refetches from the server, which is safe when you do not know the exact new shape; setQueryData patches the cache directly when you do, avoiding a round trip.
Report a problem with this articleOpens a pre-filled issue on GitHub.