Add meta.tags to the query
One line. The query is now reactive — it will refetch whenever a matching tag is invalidated from the server.
Before
// BEFORE — plain TanStack Query (no tags, nothing to change)
export function useTodos() {
return useQuery({
queryKey: ["todos"],
queryFn: fetchTodos,
})
}After
// AFTER — add one line to opt this query into tag invalidation
import { appTags } from "#/lib/tag-contract"
export function useTodos() {
return useQuery({
queryKey: ["todos"],
queryFn: fetchTodos,
meta: { tags: [appTags.todos.list()] }, // <-- new
})
}Prefer a query factory pattern? Use withTags(options, tags) — it returns a plain options object you can pass straight to useQuery.
// Alternative: withTags() wraps the whole options object
import { withTags } from "@tanstack-tools/query-tags"
import { appTags } from "#/lib/tag-contract"
export const todosQuery = withTags(
{ queryKey: ["todos"], queryFn: fetchTodos },
[appTags.todos.list()],
)
// Then: useQuery(todosQuery)
// Good for shared query factories — tags travel with the options object.