Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

useHydrate.md

75 lines2.6 KB

1---
2id: useHydrate
3title: useHydrate
4---
5​
6<!-- DO NOT EDIT: this page is autogenerated from the type comments -->
7​
8```ts
9function useHydrate(
10 state?: unknown,
11 options?: HydrateOptions,
12 queryClient?: QueryClient
13): void;
14```
15​
16Defined in: [packages/svelte-query/src/useHydrate.ts:31](https://github.com/TanStack/query/blob/main/packages/svelte-query/src/useHydrate.ts#L31)
17​
18Adds a previously dehydrated `state` into the `queryClient` (from the nearest context, or the one
19passed explicitly). If the client already contains data, the new queries will be intelligently merged based
20on update timestamp. `HydrationBoundary` wraps this β€” use it directly only if you need to hydrate from your
21own component instead.
22​
23## Parameters
24​
25### state?
26​
27`unknown`
28​
29The dehydrated state to hydrate into the cache, as produced by `dehydrate`.
30​
31### options?
32​
33[`HydrateOptions`](../interfaces/HydrateOptions.md)
34​
35[HydrateOptions](../interfaces/HydrateOptions.md) to control the hydration.
36​
37<a id="options-properties"></a>
38​
39#### `options` properties
40​
41| Property | Type | Description |
42| ------ | ------ | ------ |
43| <a id="options-property-defaultoptions"></a> `defaultOptions?` | `object` | Options applied to the queries and mutations restored from the dehydrated state. |
44| `defaultOptions.deserializeData?` | `TransformerFn` | Transforms a query's `data` after it is read from the dehydrated state, reversing `serializeData`. |
45| `defaultOptions.mutations?` | [`MutationOptions`](../interfaces/MutationOptions.md)\<`unknown`, `Error`, `unknown`, `unknown`\> | Default options merged into every mutation restored from the dehydrated state. |
46| `defaultOptions.queries?` | [`QueryOptions`](../interfaces/QueryOptions.md)\<`unknown`, `Error`, `unknown`, readonly `unknown`[], `never`\> | Default options merged into every query restored from the dehydrated state. |
47​
48### queryClient?
49​
50[`QueryClient`](../classes/QueryClient.md)
51​
52Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
53be used.
54​
55## Returns
56​
57`void`
58​
59## Example
60​
61Server-side prefetch handed off to the client via `dehydrate` β€” `dehydratedState` would typically come
62from a server load function that prefetched with `queryClient.query` and called `dehydrate(queryClient)`:
63```svelte
64<script lang="ts">
65 import { HydrationBoundary } from '@tanstack/svelte-query'
66 import type { DehydratedState } from '@tanstack/svelte-query'
67 import Posts from './Posts.svelte'
68​
69 let { dehydratedState }: { dehydratedState: DehydratedState } = $props()
70</script>
71​
72<HydrationBoundary state={dehydratedState}>
73 <Posts />
74</HydrationBoundary>
75```