You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Typesafe useQuery, useInfiniteQuery with default suspense option.
7
+
8
+
Use @suspensive/react-query, delegate loading and error handling to the outside of the component with useSuspenseQuery and useSuspenseInfiniteQuery, and focus on success inside the component.
9
+
10
+
You don't even need to use the isSuccess flag.
11
+
12
+
## Installation
13
+
You can install @suspensive/react-query via [NPM](https://www.npmjs.com/package/@suspensive/react-query).
If you turn suspense mode on in @tanstack/react-query, You can use useQuery with Suspense and ErrorBoundary.
26
+
27
+
```tsx
28
+
import { useQuery } from'@tanstack/react-query'
29
+
30
+
const Example = () => {
31
+
const query =useQuery(queryKey, queryFn, {
32
+
suspense: true,
33
+
})
34
+
35
+
query.data// TData | undefined
36
+
37
+
if (query.isSuccess) {
38
+
query.data// TData
39
+
}
40
+
}
41
+
```
42
+
43
+
Typically query.data will be `TData | undefined` like this code example.
44
+
But actual useQuery's return type:query.data will be always fulfilled because of [Suspense](https://suspensive.org/docs/react/src/Suspense.i18n) and [ErrorBoundary](https://suspensive.org/docs/react/src/ErrorBoundary.i18n) as parent of this component.
45
+
46
+
This is why @suspensive/react-query provide **useSuspenseQuery**
47
+
48
+
## useSuspenseQuery
49
+
50
+
Return type of this hook have no isLoading, isError property. because Suspense and ErrorBoundary will guarantee this hook's data.
51
+
52
+
In addition, this hook's options have default suspense: true. and you can provide new options to this hook like useQuery of @tanstack/react-query.
Copy file name to clipboardExpand all lines: docs/react/guides/ssr.md
+14-11Lines changed: 14 additions & 11 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -302,19 +302,19 @@ ReactDOM.hydrate(
302
302
303
303
## Using the `app` Directory in Next.js 13
304
304
305
-
Both prefetching approaches, using `initialData` or `<Hydrate>`, are available within the `app` directory.
305
+
Both prefetching approaches, using `initialData` or `<HydrationBoundary>`, are available within the `app` directory.
306
306
307
307
- Prefetch the data in a Server Component and prop drill `initialData` to Client Components
308
308
- Quick to set up for simple cases
309
309
- May need to prop drill through multiple layers of Client Components
310
310
- May need to prop drill to multiple Client Components using the same query
311
311
- Query refetching is based on when the page loads instead of when the data was prefetched on the server
312
-
- Prefetch the query on the server, dehydrate the cache and rehydrate it on the client with `<Hydrate>`
312
+
- Prefetch the query on the server, dehydrate the cache and rehydrate it on the client with `<HydrationBoundary>`
313
313
- Requires slightly more setup up front
314
314
- No need to prop drill
315
315
- Query refetching is based on when the query was prefetched on the server
316
316
317
-
### `<QueryClientProvider>` is required by both the `initialData` and `<Hydrate>` prefetching approaches
317
+
### `<QueryClientProvider>` is required by both the `initialData` and `<HydrationBoundary>` prefetching approaches
318
318
319
319
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`.
320
320
@@ -379,7 +379,7 @@ export function Posts(props) {
379
379
}
380
380
```
381
381
382
-
### Using `<Hydrate>`
382
+
### Using `<HydrationBoundary>`
383
383
384
384
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.**
385
385
@@ -397,30 +397,33 @@ Fetch your data in a Server Component higher up in the component tree than the C
397
397
- Retrieve the `QueryClient` singleton instance
398
398
- Prefetch the data using the client's prefetchQuery method and wait for it to complete
399
399
- Use `dehydrate` to obtain the dehydrated state of the prefetched queries from the query cache
400
-
- Wrap the component tree that needs the prefetched queries inside `<Hydrate>`, and provide it with the dehydrated state
401
-
- You can fetch inside multiple Server Components and use `<Hydrate>` in multiple places
400
+
- Wrap the component tree that needs the prefetched queries inside `<HydrationBoundary>`, and provide it with the dehydrated state
401
+
- You can fetch inside multiple Server Components and use `<HydrationBoundary>` in multiple places
402
402
403
403
> 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.
During server rendering, calls to `useQuery` nested within the `<Hydrate>` Client Component will have access to prefetched data provided in the state property.
426
+
During server rendering, calls to `useQuery` nested within the `<HydrationBoundary>` Client Component will have access to prefetched data provided in the state property.
0 commit comments