Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change Hydrate->HydrationBoundary #5455

Merged
merged 3 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions docs/react/guides/ssr.md
Original file line number Diff line number Diff line change
Expand Up @@ -302,19 +302,19 @@ ReactDOM.hydrate(

## Using the `app` Directory in Next.js 13

Both prefetching approaches, using `initialData` or `<Hydrate>`, are available within the `app` directory.
Both prefetching approaches, using `initialData` or `<HydrationBoundary>`, are available within the `app` directory.

- Prefetch the data in a Server Component and prop drill `initialData` to Client Components
- Quick to set up for simple cases
- May need to prop drill through multiple layers of Client Components
- May need to prop drill to multiple Client Components using the same query
- Query refetching is based on when the page loads instead of when the data was prefetched on the server
- Prefetch the query on the server, dehydrate the cache and rehydrate it on the client with `<Hydrate>`
- Prefetch the query on the server, dehydrate the cache and rehydrate it on the client with `<HydrationBoundary>`
- Requires slightly more setup up front
- No need to prop drill
- Query refetching is based on when the query was prefetched on the server

### `<QueryClientProvider>` is required by both the `initialData` and `<Hydrate>` prefetching approaches
### `<QueryClientProvider>` is required by both the `initialData` and `<HydrationBoundary>` prefetching approaches

The hooks provided by the `react-query` package need to retrieve a `QueryClient` from their context. Wrap your component tree with `<QueryClientProvider>` and pass it an instance of `QueryClient`.

Expand Down Expand Up @@ -379,7 +379,7 @@ export function Posts(props) {
}
```

### Using `<Hydrate>`
### Using `<HydrationBoundary>`

Create a request-scoped singleton instance of `QueryClient`. **This ensures that data is not shared between different users and requests, while still only creating the QueryClient once per request.**

Expand All @@ -397,30 +397,33 @@ Fetch your data in a Server Component higher up in the component tree than the C
- Retrieve the `QueryClient` singleton instance
- Prefetch the data using the client's prefetchQuery method and wait for it to complete
- Use `dehydrate` to obtain the dehydrated state of the prefetched queries from the query cache
- Wrap the component tree that needs the prefetched queries inside `<Hydrate>`, and provide it with the dehydrated state
- You can fetch inside multiple Server Components and use `<Hydrate>` in multiple places
- Wrap the component tree that needs the prefetched queries inside `<HydrationBoundary>`, and provide it with the dehydrated state
- You can fetch inside multiple Server Components and use `<HydrationBoundary>` in multiple places

> NOTE: TypeScript currently complains of a type error when using async Server Components. As a temporary workaround, use `{/* @ts-expect-error Server Component */}` when calling this component inside another. For more information, see [End-to-End Type Safety](https://beta.nextjs.org/docs/configuring/typescript#end-to-end-type-safety) in the Next.js 13 beta docs.

```tsx
// app/hydratedPosts.jsx
TkDodo marked this conversation as resolved.
Show resolved Hide resolved
import { dehydrate, Hydrate } from '@tanstack/react-query'
import { dehydrate, HydrationBoundary } from '@tanstack/react-query'
import getQueryClient from './getQueryClient'

export default async function HydratedPosts() {
const queryClient = getQueryClient()
await queryClient.prefetchQuery(['posts'], getPosts)

await queryClient.prefetchQuery({querykey:['posts'], queryFn:getPosts})
// for infinite queries with useInfiniteQuery use
// await queryClient.prefetchInfiniteQuery({queryKey:['posts'], queryFn:getPosts,...})
const dehydratedState = dehydrate(queryClient)

return (
<Hydrate state={dehydratedState}>
<HydrationBoundary state={dehydratedState}>
<Posts />
</Hydrate>
</HydrationBoundary>
)
}
```

During server rendering, calls to `useQuery` nested within the `<Hydrate>` Client Component will have access to prefetched data provided in the state property.
During server rendering, calls to `useQuery` nested within the `<HydrationBoundary>` Client Component will have access to prefetched data provided in the state property.

```tsx
// app/posts.jsx
Expand Down
2 changes: 1 addition & 1 deletion docs/svelte/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ Svelte Query offers useful functions and components that will make managing serv
- `useIsMutating`
- `useHydrate`
- `<QueryClientProvider>`
- `<Hydrate>`
- `<HydrationBoundary>`

## Important Differences between Svelte Query & React Query

Expand Down