Select successful queries for transfer to the client
Server-side rendering
TanStack · Const
defaultShouldDehydrateQuery is TanStack Query's default predicate for selecting queries during dehydration. It includes only queries with a successful status.
This predicate receives each query considered during dehydration and returns whether it should be included. Its default rule accepts only queries whose status is "success", so custom predicates can call it first to preserve that behavior while adding other conditions.
function defaultShouldDehydrateQuery(query: Query): boolean;| Name | Type | Description |
|---|---|---|
query | Query | The Query whose dehydration eligibility is evaluated. |
boolean: true when the query's status is 'success'.
import { dehydrate, defaultShouldDehydrateQuery } from '@tanstack/query-core'
const state = dehydrate(queryClient, {
shouldDehydrateQuery: (query) =>
defaultShouldDehydrateQuery(query) || query.state.status === 'error',
})Creates dehydrated state that keeps the default successful queries and additionally includes failed queries.
Server-side rendering
Error-aware hydration
Custom dehydration policies
It is the default predicate used by dehydrate to decide whether a query is included in dehydrated state. It returns true only when the query status is "success".
Follow-up: How would you include errors while retaining the default behavior? Call this predicate and OR its result with a condition for the error status.
The function accepts one Query argument and returns a boolean. A true result means the query meets the default dehydration rule.
Follow-up: Why might a caller avoid replacing the predicate with an unconditional true? Because that changes the default filtering and includes queries that are not successful.
dehydrate uses this predicate as its default query-selection rule. To extend the rule, provide shouldDehydrateQuery and invoke the default predicate inside the custom function.
Follow-up: What additional behavior can the custom predicate implement? It can selectively include other query statuses, such as errors, while preserving successful-query inclusion.
dehydrateDehydrateOptionsshouldDehydrateQuery