Filters
Source URL: https://tanstack.com/query/latest/docs/framework/react/guides/filters
Filters
Section titled “Filters”Some methods within TanStack Query accept a QueryFilters or MutationFilters object.
Query Filters
Section titled “Query Filters”A query filter is an object with certain conditions to match a query with:
// Cancel all queriesawait queryClient.cancelQueries()
// Remove all inactive queries that begin with `posts` in the keyqueryClient.removeQueries({ queryKey: ['posts'], type: 'inactive' })
// Refetch all active queriesawait queryClient.refetchQueries({ type: 'active' })
// Refetch all active queries that begin with `posts` in the keyawait queryClient.refetchQueries({ queryKey: ['posts'], type: 'active' })A query filter object supports the following properties:
queryKey?: QueryKey- Set this property to define a query key to match on.
exact?: boolean- If you don’t want to search queries inclusively by query key, you can pass the
exact: trueoption to return only the query with the exact query key you have passed.
- If you don’t want to search queries inclusively by query key, you can pass the
type?: 'active' | 'inactive' | 'all'- Defaults to
all - When set to
activeit will match active queries. - When set to
inactiveit will match inactive queries.
- Defaults to
stale?: boolean- When set to
trueit will match stale queries. - When set to
falseit will match fresh queries.
- When set to
fetchStatus?: FetchStatus- When set to
fetchingit will match queries that are currently fetching. - When set to
pausedit will match queries that wanted to fetch, but have beenpaused. - When set to
idleit will match queries that are not fetching.
- When set to
predicate?: (query: Query) => boolean- This predicate function will be used as a final filter on all matching queries. If no other filters are specified, this function will be evaluated against every query in the cache.
Mutation Filters
Section titled “Mutation Filters”A mutation filter is an object with certain conditions to match a mutation with:
// Get the number of all fetching mutationsawait queryClient.isMutating()
// Filter mutations by mutationKeyawait queryClient.isMutating({ mutationKey: ['post'] })
// Filter mutations using a predicate functionawait queryClient.isMutating({ predicate: (mutation) => mutation.state.variables?.id === 1,})A mutation filter object supports the following properties:
mutationKey?: MutationKey- Set this property to define a mutation key to match on.
exact?: boolean- If you don’t want to search mutations inclusively by mutation key, you can pass the
exact: trueoption to return only the mutation with the exact mutation key you have passed.
- If you don’t want to search mutations inclusively by mutation key, you can pass the
status?: MutationStatus- Allows for filtering mutations according to their status.
predicate?: (mutation: Mutation) => boolean- This predicate function will be used as a final filter on all matching mutations. If no other filters are specified, this function will be evaluated against every mutation in the cache.
matchQuery
Section titled “matchQuery”const isMatching = matchQuery(filters, query)Returns a boolean that indicates whether a query matches the provided set of query filters.
matchMutation
Section titled “matchMutation”const isMatching = matchMutation(filters, mutation)Returns a boolean that indicates whether a mutation matches the provided set of mutation filters.