|
| 1 | +--- |
| 2 | +id: hydration |
| 3 | +title: hydration |
| 4 | +--- |
| 5 | + |
| 6 | +## `dehydrate` |
| 7 | + |
| 8 | +`dehydrate` 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. |
| 9 | + |
| 10 | +```js |
| 11 | +import { dehydrate } from 'react-query' |
| 12 | + |
| 13 | +const dehydratedState = dehydrate(queryClient, { |
| 14 | + shouldDehydrateQuery, |
| 15 | +}) |
| 16 | +``` |
| 17 | + |
| 18 | +> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `dehydrate` from `react-query/hydration` |
| 19 | +
|
| 20 | +**Options** |
| 21 | + |
| 22 | +- `client: QueryClient` |
| 23 | + - **Required** |
| 24 | + - The `queryClient` that should be dehydrated |
| 25 | +- `options: DehydrateOptions` |
| 26 | + - Optional |
| 27 | + - `dehydrateMutations: boolean` |
| 28 | + - Optional |
| 29 | + - Whether or not to dehydrate mutations. |
| 30 | + - `dehydrateQueries: boolean` |
| 31 | + - Optional |
| 32 | + - Whether or not to dehydrate queries. |
| 33 | + - `shouldDehydrateMutation: (mutation: Mutation) => boolean` |
| 34 | + - Optional |
| 35 | + - This function is called for each mutation in the cache |
| 36 | + - Return `true` to include this mutation in dehydration, or `false` otherwise |
| 37 | + - The default version only includes paused mutations |
| 38 | + - `shouldDehydrateQuery: (query: Query) => boolean` |
| 39 | + - Optional |
| 40 | + - This function is called for each query in the cache |
| 41 | + - Return `true` to include this query in dehydration, or `false` otherwise |
| 42 | + - The default version only includes successful queries, do `shouldDehydrateQuery: () => true` to include all queries |
| 43 | + |
| 44 | +**Returns** |
| 45 | + |
| 46 | +- `dehydratedState: DehydratedState` |
| 47 | + - This includes everything that is needed to hydrate the `queryClient` at a later point |
| 48 | + - You **should not** rely on the exact format of this response, it is not part of the public API and can change at any time |
| 49 | + - This result is not in serialized form, you need to do that yourself if desired |
| 50 | + |
| 51 | +## `hydrate` |
| 52 | + |
| 53 | +`hydrate` adds a previously dehydrated state into a `cache`. If the queries included in dehydration already exist in the queryCache, `hydrate` does not overwrite them. |
| 54 | + |
| 55 | +```js |
| 56 | +import { hydrate } from 'react-query' |
| 57 | + |
| 58 | +hydrate(queryClient, dehydratedState, options) |
| 59 | +``` |
| 60 | + |
| 61 | +> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `hydrate` from `react-query/hydration` |
| 62 | +
|
| 63 | +**Options** |
| 64 | + |
| 65 | +- `client: QueryClient` |
| 66 | + - **Required** |
| 67 | + - The `queryClient` to hydrate the state into |
| 68 | +- `dehydratedState: DehydratedState` |
| 69 | + - **Required** |
| 70 | + - The state to hydrate into the client |
| 71 | +- `options: HydrateOptions` |
| 72 | + - Optional |
| 73 | + - `defaultOptions: DefaultOptions` |
| 74 | + - Optional |
| 75 | + - `mutations: MutationOptions` The default mutation options to use for the hydrated mutations. |
| 76 | + - `queries: QueryOptions` The default query options to use for the hydrated queries. |
| 77 | + |
| 78 | +## `useHydrate` |
| 79 | + |
| 80 | +`useHydrate` 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. |
| 81 | + |
| 82 | +```jsx |
| 83 | +import { useHydrate } from 'react-query' |
| 84 | + |
| 85 | +useHydrate(dehydratedState, options) |
| 86 | +``` |
| 87 | + |
| 88 | +> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `useHydrate` from `react-query/hydration` |
| 89 | +
|
| 90 | +**Options** |
| 91 | + |
| 92 | +- `dehydratedState: DehydratedState` |
| 93 | + - **Required** |
| 94 | + - The state to hydrate |
| 95 | +- `options: HydrateOptions` |
| 96 | + - Optional |
| 97 | + - `defaultOptions: QueryOptions` |
| 98 | + - The default query options to use for the hydrated queries. |
| 99 | + |
| 100 | +## `Hydrate` |
| 101 | + |
| 102 | +`Hydrate` wraps `useHydrate` into component. Can be useful when you need hydrate in class component or need hydrate on same level where `QueryClientProvider` rendered. |
| 103 | + |
| 104 | +```js |
| 105 | +import { Hydrate } from 'react-query' |
| 106 | + |
| 107 | +function App() { |
| 108 | + return <Hydrate state={dehydratedState}>...</Hydrate> |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +> Note: Since version `3.22.0` hydration utilities moved into to core. If you using lower version your should import `Hydrate` from `react-query/hydration` |
| 113 | +
|
| 114 | +**Options** |
| 115 | + |
| 116 | +- `state: DehydratedState` |
| 117 | + - The state to hydrate |
| 118 | +- `options: HydrateOptions` |
| 119 | + - Optional |
| 120 | + - `defaultOptions: QueryOptions` |
| 121 | + - The default query options to use for the hydrated queries. |
0 commit comments