Skip to main content
The hydration utilities allow you to serialize and deserialize query and mutation state for server-side rendering (SSR) and other use cases where you need to transfer state between different contexts.

dehydrate

Serializes the state of queries and mutations from a QueryClient into a plain JavaScript object that can be transferred or stored.

Parameters

QueryClient
required
The QueryClient instance whose state you want to dehydrate.
DehydrateOptions
Optional configuration for the dehydration process.
(data: any) => any
Function to transform data before dehydration. Useful for serializing non-JSON-serializable data.
(mutation: Mutation) => boolean
Predicate function to determine which mutations should be included in the dehydrated state.Default behavior: Only includes paused mutations (mutation.state.isPaused === true).
(query: Query) => boolean
Predicate function to determine which queries should be included in the dehydrated state.Default behavior: Only includes successful queries (query.state.status === 'success').
(error: unknown) => boolean
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

object
An object containing the serialized state of queries and mutations.
Array<DehydratedQuery>
Array of dehydrated query objects.
Array<DehydratedMutation>
Array of dehydrated mutation objects.

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

QueryClient
required
The QueryClient instance to hydrate with the dehydrated state.
unknown
required
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.
HydrateOptions
Optional configuration for the hydration process.
object
Default options to apply to hydrated queries and mutations.
(data: any) => any
Function to transform data after hydration. Should be the inverse of serializeData used in dehydrate.
QueryOptions
Default options to merge with hydrated query options.
MutationOptions<unknown, DefaultError, unknown, unknown>
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 fetchStatus is 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.
Returns: true if the query’s status is 'success', false otherwise.

defaultShouldDehydrateMutation

The default predicate function used to determine if a mutation should be dehydrated.
Returns: true if the mutation is paused (mutation.state.isPaused === true), false otherwise.

TypeScript Types

DehydrateOptions

HydrateOptions

DehydratedState


Common Use Cases

Server-Side Rendering (SSR)

Persisting to Local Storage