Query and Mutation Matching
matchQuery
Determines if a query matches the given filters.Parameters
QueryFilters
required
The filter criteria to match against.
'all' | 'active' | 'inactive'
default:"'all'"
Filter queries by their active state.
boolean
If
true, the query key must match exactly. If false, partial matching is used.QueryKey
The query key or key prefix to match.
boolean
Filter queries by their stale state.
'fetching' | 'paused' | 'idle'
Filter queries by their fetch status.
(query: Query) => boolean
Custom predicate function for advanced filtering.
Query
required
The query instance to test against the filters.
Returns
boolean - Returns true if the query matches all specified filters, false otherwise.
matchMutation
Determines if a mutation matches the given filters.Parameters
MutationFilters
required
The filter criteria to match against.
boolean
If
true, the mutation key must match exactly. If false, partial matching is used.MutationKey
The mutation key or key prefix to match.
'idle' | 'pending' | 'success' | 'error'
Filter mutations by their status.
(mutation: Mutation) => boolean
Custom predicate function for advanced filtering.
Mutation
required
The mutation instance to test against the filters.
Returns
boolean - Returns true if the mutation matches all specified filters, false otherwise.
Key Hashing and Matching
hashKey
Generates a stable hash from a query or mutation key.Parameters
QueryKey | MutationKey
required
The key to hash. Can be any serializable array.
Returns
string - A stable JSON string representation of the key with object keys sorted.
Example
Object properties in the key are sorted alphabetically to ensure consistent hashing regardless of property order.
hashQueryKeyByOptions
Generates a query key hash using options-specific hash function if provided.Parameters
QueryKey
required
The query key to hash.
Pick<QueryOptions, 'queryKeyHashFn'>
Optional query options containing a custom hash function.
Returns
string - The hashed query key using the custom hash function if provided, or the default hashKey function.
partialMatchKey
Checks if keyb partially matches with key a.
Parameters
QueryKey
required
The full key to match against.
QueryKey
required
The partial key or key prefix to check.
Returns
boolean - Returns true if b is a partial match of a.
Example
Data Transformation
replaceEqualDeep
Performs deep equality checking and structural sharing between two values.Parameters
unknown
required
The previous value to compare against.
T
required
The new value to compare and potentially share structure with.
number
default:"0"
The current recursion depth. Stops recursion at depth 500 to prevent stack overflow.
Returns
T - Returns a if b is deeply equal, otherwise returns a new object/array with shared references for equal children.
Example
This function is used internally by TanStack Query for structural sharing, which helps prevent unnecessary re-renders by maintaining referential equality for unchanged data.
replaceData
Replaces query data with structural sharing based on options.Parameters
TData | undefined
required
The previous data value.
TData
required
The new data value.
QueryOptions
required
Query options containing
structuralSharing configuration.Returns
TData - The new data, potentially with structural sharing applied.
keepPreviousData
Utility function that returns the previous data unchanged.Parameters
T | undefined
required
The previous data value.
Returns
T | undefined - The same previous data value passed in.
Example
Object Comparison
shallowEqualObjects
Performs shallow equality comparison between two objects.Parameters
Record<string, any>
required
The first object to compare.
Record<string, any> | undefined
required
The second object to compare.
Returns
boolean - Returns true if objects have the same keys and values (using === comparison), false otherwise.
Type Guards
isPlainArray
Checks if a value is a plain array.Returns
boolean - Returns true if the value is an array with only numeric indices.
isPlainObject
Checks if a value is a plain JavaScript object.Returns
boolean - Returns true if the value is a plain object created by {} or new Object(), false for class instances, arrays, null, etc.
isValidTimeout
Checks if a value is a valid timeout duration.Returns
boolean - Returns true if the value is a number >= 0 and not Infinity.
Time Utilities
timeUntilStale
Calculates the time in milliseconds until data becomes stale.Parameters
number
required
The timestamp when the data was last updated (in milliseconds).
number
The stale time duration in milliseconds.
Returns
number - The number of milliseconds until the data becomes stale, or 0 if already stale.
resolveStaleTime
Resolves a stale time value, handling both static values and functions.Returns
number | 'static' | undefined - The resolved stale time value.
resolveEnabled
Resolves an enabled value, handling both static values and functions.Returns
boolean | undefined - The resolved enabled value.
sleep
Creates a promise that resolves after a specified timeout.Parameters
number
required
The timeout duration in milliseconds.
Returns
Promise<void> - A promise that resolves after the specified timeout.
Array Utilities
addToEnd
Adds an item to the end of an array with optional maximum length.Parameters
Array<T>
required
The source array.
T
required
The item to add.
number
default:"0"
Maximum array length. If exceeded, items are removed from the start. Use
0 for no limit.Returns
Array<T> - A new array with the item added to the end.
addToStart
Adds an item to the start of an array with optional maximum length.Parameters
Array<T>
required
The source array.
T
required
The item to add.
number
default:"0"
Maximum array length. If exceeded, items are removed from the end. Use
0 for no limit.Returns
Array<T> - A new array with the item added to the start.
Functional Utilities
functionalUpdate
Applies an updater that can be either a value or a function.Parameters
TOutput | ((input: TInput) => TOutput)
required
Either a direct value or a function that takes the input and returns the output.
TInput
required
The input value to pass to the updater function.
Returns
TOutput - If updater is a function, returns the result of calling it with input. Otherwise, returns updater directly.
noop
A no-operation function that does nothing.Example
ensureQueryFn
Ensures a valid query function exists or returns an appropriate fallback.Returns
QueryFunction - A valid query function, or a function that returns a rejected promise if no valid function is available.
shouldThrowError
Determines if an error should be thrown based on configuration.Parameters
boolean | Function | undefined
required
Can be a boolean, a function, or undefined.
Array<any>
required
Parameters to pass to the function if
throwOnError is a function.Returns
boolean - Returns true if errors should be thrown.
Constants
skipToken
A special symbol used to skip query execution.Example
isServer
Boolean indicating if the code is running in a server environment.true if window is undefined or Deno is detected in global scope.