docs/framework/react/reference/hydration.md
dehydratedehydrate creates a frozen representation of a cache that can later be hydrated with HydrationBoundary or hydrate. This is useful for passing prefetched queries from server to client or persisting queries to localStorage or other persistent locations. It only includes currently successful queries by default.
import { dehydrate } from '@tanstack/react-query'
const dehydratedState = dehydrate(queryClient, {
shouldDehydrateQuery,
shouldDehydrateMutation,
})
Options
client: QueryClient
queryClient that should be dehydratedoptions: DehydrateOptions
shouldDehydrateMutation: (mutation: Mutation) => boolean
true to include this mutation in dehydration, or false otherwisedefaultShouldDehydrateMutation as part of the return statementshouldDehydrateQuery: (query: Query) => boolean
true to include this query in dehydration, or false otherwisedefaultShouldDehydrateQuery as part of the return statementserializeData?: (data: any) => any A function to transform (serialize) data during dehydration.shouldRedactErrors?: (error: unknown) => boolean
true to redact this error, or false otherwiseReturns
dehydratedState: DehydratedState
queryClient at a later pointSome storage systems (such as browser Web Storage API) require values to be JSON serializable. If you need to dehydrate values that are not automatically serializable to JSON (like Error or undefined), you have to serialize them for yourself. Since only successful queries are included per default, to also include Errors, you have to provide shouldDehydrateQuery, e.g.:
// server
const state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errors
const serializedState = mySerialize(state) // transform Error instances to objects
// client
const state = myDeserialize(serializedState) // transform objects back to Error instances
hydrate(client, state)
hydratehydrate adds a previously dehydrated state into a cache.
import { hydrate } from '@tanstack/react-query'
hydrate(queryClient, dehydratedState, options)
Options
client: QueryClient
queryClient to hydrate the state intodehydratedState: DehydratedState
options: HydrateOptions
defaultOptions: DefaultOptions
mutations: MutationOptions The default mutation options to use for the hydrated mutations.queries: QueryOptions The default query options to use for the hydrated queries.deserializeData?: (data: any) => any A function to transform (deserialize) data before it is put into the cache.queryClient?: QueryClient
If the queries you're trying to hydrate already exist in the queryCache, hydrate will only overwrite them if the data is newer than the data present in the cache. Otherwise, it will not get applied.
HydrationBoundaryHydrationBoundary adds a previously dehydrated state into the queryClient that would be returned by useQueryClient(). If the client already contains data, the new queries will be intelligently merged based on update timestamp.
import { HydrationBoundary } from '@tanstack/react-query'
function App() {
return <HydrationBoundary state={dehydratedState}>...</HydrationBoundary>
}
Note: Only
queriescan be dehydrated with anHydrationBoundary.
Options
state: DehydratedState
options: HydrateOptions
defaultOptions: QueryOptions
queryClient?: QueryClient