dehydrate
Serializes the state of queries and mutations from a QueryClient into a plain JavaScript object that can be transferred or stored.Parameters
The QueryClient instance whose state you want to dehydrate.
Optional configuration for the dehydration process.
Function to transform data before dehydration. Useful for serializing non-JSON-serializable data.
Predicate function to determine which mutations should be included in the dehydrated state.Default behavior: Only includes paused mutations (
mutation.state.isPaused === true).Predicate function to determine which queries should be included in the dehydrated state.Default behavior: Only includes successful queries (
query.state.status === 'success').Predicate function to determine if errors should be redacted in pending queries.Default behavior: Always redacts errors (
true). Redacted errors are replaced with new Error('redacted') in production builds.Returns
Example
By default,
dehydrate only includes successful queries. If you need to dehydrate queries in other states (pending, error), you must provide a custom shouldDehydrateQuery function.hydrate
Deserializes dehydrated state and adds it to a QueryClient, restoring queries and mutations.Parameters
The QueryClient instance to hydrate with the dehydrated state.
The dehydrated state object, typically obtained from the
dehydrate function. If the value is not an object or is null, the function returns early without doing anything.Optional configuration for the hydration process.
Default options to apply to hydrated queries and mutations.
Function to transform data after hydration. Should be the inverse of
serializeData used in dehydrate.Default options to merge with hydrated query options.
Default options to merge with hydrated mutation options.
Returns
void - This function does not return a value. It modifies the QueryClient in place.
Example
Behavior
When hydrating, TanStack Query intelligently merges the dehydrated state with any existing queries:
- If a query already exists with newer data, the hydrated data is ignored
- If the hydrated state has newer data (based on
dataUpdatedAt), it updates the existing query - If the query doesn’t exist, it creates a new query with the hydrated state
- The
fetchStatusis always reset to'idle'to avoid queries being stuck in a fetching state
Helper Functions
defaultShouldDehydrateQuery
The default predicate function used to determine if a query should be dehydrated.true if the query’s status is 'success', false otherwise.
defaultShouldDehydrateMutation
The default predicate function used to determine if a mutation should be dehydrated.true if the mutation is paused (mutation.state.isPaused === true), false otherwise.