
dehydratedehydrate creates a frozen representation of a cache that can later be hydrated with Hydrate, useHydrate, 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 'react-query'const dehydratedState = dehydrate(queryClient, {shouldDehydrateQuery,})
Note: Since version
3.22.0hydration utilities moved into to core. If you using lower version your should importdehydratefromreact-query/hydration
Options
client: QueryClientqueryClient that should be dehydratedoptions: DehydrateOptionsdehydrateMutations: booleandehydrateQueries: booleanshouldDehydrateMutation: (mutation: Mutation) => booleantrue to include this mutation in dehydration, or false otherwiseshouldDehydrateQuery: (query: Query) => booleantrue to include this query in dehydration, or false otherwiseshouldDehydrateQuery: () => true to include all queriesReturns
dehydratedState: DehydratedStatequeryClient at a later pointThe hydration API requires 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.:
// serverconst state = dehydrate(client, { shouldDehydrateQuery: () => true }) // to also include Errorsconst serializedState = mySerialize(state) // transform Error instances to objects// clientconst state = myDeserialize(serializedState) // transform objects back to Error instanceshydrate(client, state)
hydratehydrate adds a previously dehydrated state into a cache. If the queries included in dehydration already exist in the queryCache, hydrate does not overwrite them.
import { hydrate } from 'react-query'hydrate(queryClient, dehydratedState, options)
Note: Since version
3.22.0hydration utilities moved into to core. If you using lower version your should importhydratefromreact-query/hydration
Options
client: QueryClientqueryClient to hydrate the state intodehydratedState: DehydratedStateoptions: HydrateOptionsdefaultOptions: DefaultOptionsmutations: MutationOptions The default mutation options to use for the hydrated mutations.queries: QueryOptions The default query options to use for the hydrated queries.useHydrateuseHydrate 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 { useHydrate } from 'react-query'useHydrate(dehydratedState, options)
Note: Since version
3.22.0hydration utilities moved into to core. If you using lower version your should importuseHydratefromreact-query/hydration
Options
dehydratedState: DehydratedStateoptions: HydrateOptionsdefaultOptions: QueryOptionsHydrateHydrate wraps useHydrate into component. Can be useful when you need hydrate in class component or need hydrate on same level where QueryClientProvider rendered.
import { Hydrate } from 'react-query'function App() {return <Hydrate state={dehydratedState}>...</Hydrate>}
Note: Since version
3.22.0hydration utilities moved into to core. If you using lower version your should importHydratefromreact-query/hydration
Options
state: DehydratedStateoptions: HydrateOptionsdefaultOptions: QueryOptionsThe latest TanStack news, articles, and resources, sent to your inbox.