@tanstack/react-query 5.102.8 → 5.103.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/legacy/HydrationBoundary.cjs +44 -1
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.d.cts +61 -0
- package/build/legacy/HydrationBoundary.d.cts.map +1 -1
- package/build/legacy/HydrationBoundary.d.ts +61 -0
- package/build/legacy/HydrationBoundary.d.ts.map +1 -1
- package/build/legacy/HydrationBoundary.js +44 -1
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/IsRestoringProvider.cjs +11 -0
- package/build/legacy/IsRestoringProvider.cjs.map +1 -1
- package/build/legacy/IsRestoringProvider.d.cts +11 -0
- package/build/legacy/IsRestoringProvider.d.cts.map +1 -1
- package/build/legacy/IsRestoringProvider.d.ts +11 -0
- package/build/legacy/IsRestoringProvider.d.ts.map +1 -1
- package/build/legacy/IsRestoringProvider.js +11 -0
- package/build/legacy/IsRestoringProvider.js.map +1 -1
- package/build/legacy/QueryClientProvider.cjs +30 -0
- package/build/legacy/QueryClientProvider.cjs.map +1 -1
- package/build/legacy/QueryClientProvider.d.cts +41 -0
- package/build/legacy/QueryClientProvider.d.cts.map +1 -1
- package/build/legacy/QueryClientProvider.d.ts +41 -0
- package/build/legacy/QueryClientProvider.d.ts.map +1 -1
- package/build/legacy/QueryClientProvider.js +30 -0
- package/build/legacy/QueryClientProvider.js.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.cjs +76 -0
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +77 -0
- package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.ts +77 -0
- package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.js +76 -0
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +173 -0
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +173 -0
- package/build/legacy/infiniteQueryOptions.d.ts.map +1 -1
- package/build/legacy/infiniteQueryOptions.js.map +1 -1
- package/build/legacy/mutationOptions.cjs.map +1 -1
- package/build/legacy/mutationOptions.d.cts +55 -0
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +55 -0
- package/build/legacy/mutationOptions.d.ts.map +1 -1
- package/build/legacy/mutationOptions.js.map +1 -1
- package/build/legacy/queryOptions.cjs.map +1 -1
- package/build/legacy/queryOptions.d.cts +175 -0
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +175 -0
- package/build/legacy/queryOptions.d.ts.map +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/types.cjs.map +1 -1
- package/build/legacy/types.d.cts +228 -2
- package/build/legacy/types.d.cts.map +1 -1
- package/build/legacy/types.d.ts +228 -2
- package/build/legacy/types.d.ts.map +1 -1
- package/build/legacy/types.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +292 -0
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +292 -0
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +36 -0
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +36 -0
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +36 -0
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +36 -0
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +172 -0
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +172 -0
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +172 -0
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +172 -0
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +94 -0
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +94 -0
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +94 -0
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +94 -0
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.cjs +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.cts +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.d.ts +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchInfiniteQuery.js +44 -0
- package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/legacy/usePrefetchQuery.cjs +35 -0
- package/build/legacy/usePrefetchQuery.cjs.map +1 -1
- package/build/legacy/usePrefetchQuery.d.cts +35 -0
- package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
- package/build/legacy/usePrefetchQuery.d.ts +35 -0
- package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
- package/build/legacy/usePrefetchQuery.js +35 -0
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +130 -0
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +165 -2
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +165 -2
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +130 -0
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +246 -0
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +246 -0
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +86 -0
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +320 -2
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +320 -2
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +67 -0
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +67 -0
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +67 -0
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +67 -0
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +44 -1
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.d.cts +61 -0
- package/build/modern/HydrationBoundary.d.cts.map +1 -1
- package/build/modern/HydrationBoundary.d.ts +61 -0
- package/build/modern/HydrationBoundary.d.ts.map +1 -1
- package/build/modern/HydrationBoundary.js +44 -1
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/IsRestoringProvider.cjs +11 -0
- package/build/modern/IsRestoringProvider.cjs.map +1 -1
- package/build/modern/IsRestoringProvider.d.cts +11 -0
- package/build/modern/IsRestoringProvider.d.cts.map +1 -1
- package/build/modern/IsRestoringProvider.d.ts +11 -0
- package/build/modern/IsRestoringProvider.d.ts.map +1 -1
- package/build/modern/IsRestoringProvider.js +11 -0
- package/build/modern/IsRestoringProvider.js.map +1 -1
- package/build/modern/QueryClientProvider.cjs +30 -0
- package/build/modern/QueryClientProvider.cjs.map +1 -1
- package/build/modern/QueryClientProvider.d.cts +41 -0
- package/build/modern/QueryClientProvider.d.cts.map +1 -1
- package/build/modern/QueryClientProvider.d.ts +41 -0
- package/build/modern/QueryClientProvider.d.ts.map +1 -1
- package/build/modern/QueryClientProvider.js +30 -0
- package/build/modern/QueryClientProvider.js.map +1 -1
- package/build/modern/QueryErrorResetBoundary.cjs +76 -0
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +77 -0
- package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.ts +77 -0
- package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.js +76 -0
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +173 -0
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +173 -0
- package/build/modern/infiniteQueryOptions.d.ts.map +1 -1
- package/build/modern/infiniteQueryOptions.js.map +1 -1
- package/build/modern/mutationOptions.cjs.map +1 -1
- package/build/modern/mutationOptions.d.cts +55 -0
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +55 -0
- package/build/modern/mutationOptions.d.ts.map +1 -1
- package/build/modern/mutationOptions.js.map +1 -1
- package/build/modern/queryOptions.cjs.map +1 -1
- package/build/modern/queryOptions.d.cts +175 -0
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +175 -0
- package/build/modern/queryOptions.d.ts.map +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/types.cjs.map +1 -1
- package/build/modern/types.d.cts +228 -2
- package/build/modern/types.d.cts.map +1 -1
- package/build/modern/types.d.ts +228 -2
- package/build/modern/types.d.ts.map +1 -1
- package/build/modern/types.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +292 -0
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +292 -0
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +36 -0
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +36 -0
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +36 -0
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +36 -0
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +172 -0
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +172 -0
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +172 -0
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +172 -0
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +94 -0
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +94 -0
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +94 -0
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +94 -0
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.cjs +44 -0
- package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.cts +44 -0
- package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.d.ts +44 -0
- package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchInfiniteQuery.js +44 -0
- package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
- package/build/modern/usePrefetchQuery.cjs +35 -0
- package/build/modern/usePrefetchQuery.cjs.map +1 -1
- package/build/modern/usePrefetchQuery.d.cts +35 -0
- package/build/modern/usePrefetchQuery.d.cts.map +1 -1
- package/build/modern/usePrefetchQuery.d.ts +35 -0
- package/build/modern/usePrefetchQuery.d.ts.map +1 -1
- package/build/modern/usePrefetchQuery.js +35 -0
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +130 -0
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +165 -2
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +165 -2
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +130 -0
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +246 -0
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +246 -0
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +86 -0
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +86 -0
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +86 -0
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +86 -0
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +320 -2
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +320 -2
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +67 -0
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +67 -0
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +67 -0
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +67 -0
- package/build/modern/useSuspenseQuery.js.map +1 -1
- package/build/query-codemods/eslint.config.js +7 -0
- package/build/query-codemods/root.eslint.config.js +45 -0
- package/package.json +11 -11
- package/src/HydrationBoundary.tsx +61 -1
- package/src/IsRestoringProvider.ts +12 -0
- package/src/QueryClientProvider.tsx +41 -0
- package/src/QueryErrorResetBoundary.tsx +89 -0
- package/src/infiniteQueryOptions.ts +173 -0
- package/src/mutationOptions.ts +55 -0
- package/src/queryOptions.ts +175 -0
- package/src/types.ts +228 -2
- package/src/useInfiniteQuery.ts +292 -0
- package/src/useIsFetching.ts +36 -0
- package/src/useMutation.ts +172 -0
- package/src/useMutationState.ts +94 -0
- package/src/usePrefetchInfiniteQuery.tsx +44 -0
- package/src/usePrefetchQuery.tsx +35 -0
- package/src/useQueries.ts +165 -2
- package/src/useQuery.ts +246 -0
- package/src/useSuspenseInfiniteQuery.ts +86 -0
- package/src/useSuspenseQueries.ts +320 -2
- package/src/useSuspenseQuery.ts +67 -0
package/src/usePrefetchQuery.tsx
CHANGED
|
@@ -4,6 +4,41 @@ import { useQueryClient } from './QueryClientProvider'
|
|
|
4
4
|
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
|
|
5
5
|
import type { UsePrefetchQueryOptions } from './types'
|
|
6
6
|
|
|
7
|
+
/**
|
|
8
|
+
* `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
|
|
9
|
+
* a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
|
|
10
|
+
* `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
|
|
11
|
+
* `queryFn` is required unless a default query function has been defined.
|
|
12
|
+
*
|
|
13
|
+
* The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
|
|
14
|
+
* over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
|
|
15
|
+
* already there or already in flight.
|
|
16
|
+
*
|
|
17
|
+
* @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.
|
|
18
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
19
|
+
* be used.
|
|
20
|
+
* @returns `void` — nothing is returned.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* ```tsx
|
|
24
|
+
* import { Suspense } from 'react'
|
|
25
|
+
* import { usePrefetchQuery } from '@tanstack/react-query'
|
|
26
|
+
*
|
|
27
|
+
* function App() {
|
|
28
|
+
* // Fire the prefetch during render, before the suspense boundary below.
|
|
29
|
+
* usePrefetchQuery({
|
|
30
|
+
* queryKey: ['posts'],
|
|
31
|
+
* queryFn: fetchPosts,
|
|
32
|
+
* })
|
|
33
|
+
*
|
|
34
|
+
* return (
|
|
35
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
36
|
+
* <Posts />
|
|
37
|
+
* </Suspense>
|
|
38
|
+
* )
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
7
42
|
export function usePrefetchQuery<
|
|
8
43
|
TQueryFnData = unknown,
|
|
9
44
|
TError = DefaultError,
|
package/src/useQueries.ts
CHANGED
|
@@ -142,7 +142,16 @@ type GetUseQueryResult<T> =
|
|
|
142
142
|
UseQueryResult
|
|
143
143
|
|
|
144
144
|
/**
|
|
145
|
-
*
|
|
145
|
+
* The `queries` array accepted by `useQueries`. Recursively unwraps each tuple element so every entry's
|
|
146
|
+
* `queryFn`/`select`/`throwOnError` are inferred individually, up to 20 elements. An opaque array (e.g.
|
|
147
|
+
* `unknown[]`) is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls
|
|
148
|
+
* back to a single homogeneous options type.
|
|
149
|
+
*
|
|
150
|
+
* @template T - The type of the `queries` array as written at the call site.
|
|
151
|
+
* @template TResults - The internal accumulator that this type builds during recursion. It is not meant
|
|
152
|
+
* to be set explicitly.
|
|
153
|
+
* @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
|
|
154
|
+
* meant to be set explicitly.
|
|
146
155
|
*/
|
|
147
156
|
export type QueriesOptions<
|
|
148
157
|
T extends Array<any>,
|
|
@@ -184,7 +193,16 @@ export type QueriesOptions<
|
|
|
184
193
|
Array<UseQueryOptionsForUseQueries>
|
|
185
194
|
|
|
186
195
|
/**
|
|
187
|
-
*
|
|
196
|
+
* The result type returned by `useQueries`, when no `combine` is provided. Mirrors {@link QueriesOptions}: each
|
|
197
|
+
* tuple element's result type is inferred individually, up to 20 elements. A non-tuple array is mapped
|
|
198
|
+
* per-element instead, still inferring each entry individually; only past 20 elements does this fall back to a
|
|
199
|
+
* single homogeneous {@link UseQueryResult} type.
|
|
200
|
+
*
|
|
201
|
+
* @template T - The type of the `queries` array, as inferred by {@link QueriesOptions}.
|
|
202
|
+
* @template TResults - The internal accumulator that this type builds during recursion. It is not meant
|
|
203
|
+
* to be set explicitly.
|
|
204
|
+
* @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
|
|
205
|
+
* meant to be set explicitly.
|
|
188
206
|
*/
|
|
189
207
|
export type QueriesResults<
|
|
190
208
|
T extends Array<any>,
|
|
@@ -204,6 +222,136 @@ export type QueriesResults<
|
|
|
204
222
|
>
|
|
205
223
|
: { [K in keyof T]: GetUseQueryResult<T[K]> }
|
|
206
224
|
|
|
225
|
+
/**
|
|
226
|
+
* The `useQueries` hook can be used to fetch a variable number of queries.
|
|
227
|
+
*
|
|
228
|
+
* The `queries` key accepts an array with query option objects mostly identical to `useQuery` — the top-level
|
|
229
|
+
* `subscribed` option isn't accepted per query (see `placeholderData` below for another difference). A custom
|
|
230
|
+
* `QueryClient` is supplied once, as `useQueries`' own top-level second argument, rather than per query.
|
|
231
|
+
*
|
|
232
|
+
* Having the same query key more than once in the array of query objects may cause some data to be shared
|
|
233
|
+
* between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
|
|
234
|
+
* structure.
|
|
235
|
+
*
|
|
236
|
+
* The `combine` option can be used to combine the results of the queries into a single value. The result will
|
|
237
|
+
* be structurally shared to be as referentially stable as possible.
|
|
238
|
+
*
|
|
239
|
+
* @remarks The `combine` function only re-runs if it changed referentially, or if any of the query results
|
|
240
|
+
* changed. An inlined `combine` function, as shown in the example below, therefore runs on every render — wrap
|
|
241
|
+
* it in `useCallback`, or extract it to a stable function reference if it doesn't have any dependencies, to
|
|
242
|
+
* avoid that.
|
|
243
|
+
*
|
|
244
|
+
* Unlike `useQuery`, `useQueries` cannot infer the `data` argument of an _inline_ `select` from its sibling
|
|
245
|
+
* `queryFn`. Because `useQueries` infers the type of the whole `queries` array at once, the `select` parameter
|
|
246
|
+
* of a query object written inline cannot be contextually typed from that same object's `queryFn`, so it falls
|
|
247
|
+
* back to `unknown` — a [known TypeScript limitation](https://github.com/TanStack/query/issues/6556). Annotate
|
|
248
|
+
* the `select` parameter explicitly, or define the query with {@link queryOptions}, which resolves its types in
|
|
249
|
+
* a single object _before_ it reaches `useQueries`, to work around this — see the example below. The same
|
|
250
|
+
* limitation applies to {@link useSuspenseQueries}.
|
|
251
|
+
*
|
|
252
|
+
* `placeholderData` is supported here too, but unlike `useQuery`, it doesn't receive information from
|
|
253
|
+
* previously rendered queries, because the number of queries can differ between renders.
|
|
254
|
+
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
255
|
+
* will be used.
|
|
256
|
+
* @returns The combined result. Without `combine`, this is an array with all the query results, in the same
|
|
257
|
+
* order as the input. When `combine` is provided, this is the value returned by `combine` instead.
|
|
258
|
+
*
|
|
259
|
+
* @example
|
|
260
|
+
* ```tsx
|
|
261
|
+
* import { useQueries } from '@tanstack/react-query'
|
|
262
|
+
*
|
|
263
|
+
* function Posts({ ids }: { ids: Array<number> }) {
|
|
264
|
+
* const postQueries = useQueries({
|
|
265
|
+
* queries: ids.map((id) => ({
|
|
266
|
+
* queryKey: ['post', id],
|
|
267
|
+
* queryFn: () => fetchPost(id),
|
|
268
|
+
* staleTime: Infinity,
|
|
269
|
+
* })),
|
|
270
|
+
* })
|
|
271
|
+
*
|
|
272
|
+
* return (
|
|
273
|
+
* <ul>
|
|
274
|
+
* {postQueries.map((query, index) => {
|
|
275
|
+
* if (query.isPending) return <li key={ids[index]}>Loading...</li>
|
|
276
|
+
* if (query.isError) return <li key={ids[index]}>Error: {query.error.message}</li>
|
|
277
|
+
* return <li key={ids[index]}>{query.data.title}</li>
|
|
278
|
+
* })}
|
|
279
|
+
* </ul>
|
|
280
|
+
* )
|
|
281
|
+
* }
|
|
282
|
+
* ```
|
|
283
|
+
*
|
|
284
|
+
* @example
|
|
285
|
+
* Combining results into a single value:
|
|
286
|
+
* ```tsx
|
|
287
|
+
* import { useQueries } from '@tanstack/react-query'
|
|
288
|
+
*
|
|
289
|
+
* function Posts({ ids }: { ids: Array<number> }) {
|
|
290
|
+
* const { data, isPending, isError } = useQueries({
|
|
291
|
+
* queries: ids.map((id) => ({
|
|
292
|
+
* queryKey: ['post', id],
|
|
293
|
+
* queryFn: () => fetchPost(id),
|
|
294
|
+
* })),
|
|
295
|
+
* combine: (postQueries) => {
|
|
296
|
+
* return {
|
|
297
|
+
* data: postQueries.map((query) => query.data),
|
|
298
|
+
* isPending: postQueries.some((query) => query.isPending),
|
|
299
|
+
* isError: postQueries.some((query) => query.isError),
|
|
300
|
+
* }
|
|
301
|
+
* },
|
|
302
|
+
* })
|
|
303
|
+
*
|
|
304
|
+
* if (isPending) return 'Loading...'
|
|
305
|
+
* if (isError) return 'Error loading posts'
|
|
306
|
+
*
|
|
307
|
+
* return (
|
|
308
|
+
* <ul>
|
|
309
|
+
* {data.map((post) => (
|
|
310
|
+
* <li key={post?.id}>{post?.title}</li>
|
|
311
|
+
* ))}
|
|
312
|
+
* </ul>
|
|
313
|
+
* )
|
|
314
|
+
* }
|
|
315
|
+
* ```
|
|
316
|
+
*
|
|
317
|
+
* @example
|
|
318
|
+
* Typing `select` via {@link queryOptions}. Note that spreading a `queryOptions` result and overriding
|
|
319
|
+
* `select` inline still falls back to `unknown` — wrap the spread in `queryOptions` again so the override is
|
|
320
|
+
* resolved before it reaches `useQueries`:
|
|
321
|
+
* ```tsx
|
|
322
|
+
* import { queryOptions, useQueries } from '@tanstack/react-query'
|
|
323
|
+
*
|
|
324
|
+
* const postOptions = (id: number) =>
|
|
325
|
+
* queryOptions({
|
|
326
|
+
* queryKey: ['post', id],
|
|
327
|
+
* queryFn: () => fetchPost(id),
|
|
328
|
+
* })
|
|
329
|
+
*
|
|
330
|
+
* function PostTitle({ id }: { id: number }) {
|
|
331
|
+
* const [{ data: broken }] = useQueries({
|
|
332
|
+
* queries: [
|
|
333
|
+
* {
|
|
334
|
+
* ...postOptions(id),
|
|
335
|
+
* // ❌ `data` is `unknown` here
|
|
336
|
+
* select: (data) => data.title,
|
|
337
|
+
* },
|
|
338
|
+
* ],
|
|
339
|
+
* })
|
|
340
|
+
*
|
|
341
|
+
* const [{ data: fixed }] = useQueries({
|
|
342
|
+
* queries: [
|
|
343
|
+
* queryOptions({
|
|
344
|
+
* ...postOptions(id),
|
|
345
|
+
* // ✅ `data` is `Post`
|
|
346
|
+
* select: (data) => data.title,
|
|
347
|
+
* }),
|
|
348
|
+
* ],
|
|
349
|
+
* })
|
|
350
|
+
*
|
|
351
|
+
* return <h1>{fixed}</h1>
|
|
352
|
+
* }
|
|
353
|
+
* ```
|
|
354
|
+
*/
|
|
207
355
|
export function useQueries<
|
|
208
356
|
T extends Array<any>,
|
|
209
357
|
TCombinedResult = QueriesResults<T>,
|
|
@@ -212,10 +360,25 @@ export function useQueries<
|
|
|
212
360
|
queries,
|
|
213
361
|
...options
|
|
214
362
|
}: {
|
|
363
|
+
/**
|
|
364
|
+
* An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
|
|
365
|
+
* `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
|
|
366
|
+
* `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
|
|
367
|
+
* and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
|
|
368
|
+
*/
|
|
215
369
|
queries:
|
|
216
370
|
| readonly [...QueriesOptions<T>]
|
|
217
371
|
| readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]
|
|
372
|
+
/**
|
|
373
|
+
* Use this to combine the results of the queries into a single value. The result will be structurally
|
|
374
|
+
* shared to be as referentially stable as possible.
|
|
375
|
+
*/
|
|
218
376
|
combine?: (result: QueriesResults<T>) => TCombinedResult
|
|
377
|
+
/**
|
|
378
|
+
* Set this to `false` to unsubscribe this observer from updates to the query cache.
|
|
379
|
+
*
|
|
380
|
+
* @defaultValue true
|
|
381
|
+
*/
|
|
219
382
|
subscribed?: boolean
|
|
220
383
|
},
|
|
221
384
|
queryClient?: QueryClient,
|
package/src/useQuery.ts
CHANGED
|
@@ -12,6 +12,41 @@ import type {
|
|
|
12
12
|
UndefinedInitialDataOptions,
|
|
13
13
|
} from './queryOptions'
|
|
14
14
|
|
|
15
|
+
/**
|
|
16
|
+
* This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
|
|
17
|
+
*
|
|
18
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
19
|
+
* @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` set.
|
|
20
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
21
|
+
* be used.
|
|
22
|
+
* @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt
|
|
23
|
+
* fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,
|
|
24
|
+
* since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.
|
|
25
|
+
*
|
|
26
|
+
* @example
|
|
27
|
+
* ```tsx
|
|
28
|
+
* import { useQuery } from '@tanstack/react-query'
|
|
29
|
+
*
|
|
30
|
+
* function Posts() {
|
|
31
|
+
* // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
|
|
32
|
+
* // so the list stays visible alongside the error.
|
|
33
|
+
* const { data, isError, error } = useQuery({
|
|
34
|
+
* queryKey: ['posts'],
|
|
35
|
+
* queryFn: fetchPosts,
|
|
36
|
+
* initialData: [],
|
|
37
|
+
* })
|
|
38
|
+
*
|
|
39
|
+
* return (
|
|
40
|
+
* <div>
|
|
41
|
+
* {isError ? <span>Error: {error.message}</span> : null}
|
|
42
|
+
* <ul>
|
|
43
|
+
* {data.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
44
|
+
* </ul>
|
|
45
|
+
* </div>
|
|
46
|
+
* )
|
|
47
|
+
* }
|
|
48
|
+
* ```
|
|
49
|
+
*/
|
|
15
50
|
export function useQuery<
|
|
16
51
|
TQueryFnData = unknown,
|
|
17
52
|
TError = DefaultError,
|
|
@@ -22,6 +57,63 @@ export function useQuery<
|
|
|
22
57
|
queryClient?: QueryClient,
|
|
23
58
|
): DefinedUseQueryResult<TData, TError>
|
|
24
59
|
|
|
60
|
+
/**
|
|
61
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
62
|
+
* @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
|
|
63
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
64
|
+
* be used.
|
|
65
|
+
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
66
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
|
|
67
|
+
* are derived booleans for convenience.
|
|
68
|
+
*
|
|
69
|
+
* @example
|
|
70
|
+
* ```tsx
|
|
71
|
+
* import { useQuery } from '@tanstack/react-query'
|
|
72
|
+
*
|
|
73
|
+
* function Posts() {
|
|
74
|
+
* const { status, data, error, isFetching } = useQuery({
|
|
75
|
+
* queryKey: ['posts'],
|
|
76
|
+
* queryFn: fetchPosts,
|
|
77
|
+
* })
|
|
78
|
+
*
|
|
79
|
+
* if (status === 'pending') return 'Loading...'
|
|
80
|
+
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
81
|
+
*
|
|
82
|
+
* return (
|
|
83
|
+
* <div>
|
|
84
|
+
* <ul>
|
|
85
|
+
* {data.map((post) => (
|
|
86
|
+
* <li key={post.id}>{post.title}</li>
|
|
87
|
+
* ))}
|
|
88
|
+
* </ul>
|
|
89
|
+
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
90
|
+
* </div>
|
|
91
|
+
* )
|
|
92
|
+
* }
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* The same query, checking `isPending`/`isError` instead of `status` — pick whichever reads better to you:
|
|
97
|
+
* ```tsx
|
|
98
|
+
* import { useQuery } from '@tanstack/react-query'
|
|
99
|
+
*
|
|
100
|
+
* function Posts() {
|
|
101
|
+
* const { isPending, isError, data, error } = useQuery({
|
|
102
|
+
* queryKey: ['posts'],
|
|
103
|
+
* queryFn: fetchPosts,
|
|
104
|
+
* })
|
|
105
|
+
*
|
|
106
|
+
* if (isPending) return 'Loading...'
|
|
107
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
108
|
+
*
|
|
109
|
+
* return (
|
|
110
|
+
* <ul>
|
|
111
|
+
* {data.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
112
|
+
* </ul>
|
|
113
|
+
* )
|
|
114
|
+
* }
|
|
115
|
+
* ```
|
|
116
|
+
*/
|
|
25
117
|
export function useQuery<
|
|
26
118
|
TQueryFnData = unknown,
|
|
27
119
|
TError = DefaultError,
|
|
@@ -32,6 +124,160 @@ export function useQuery<
|
|
|
32
124
|
queryClient?: QueryClient,
|
|
33
125
|
): UseQueryResult<TData, TError>
|
|
34
126
|
|
|
127
|
+
/**
|
|
128
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
129
|
+
* @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.
|
|
130
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
131
|
+
* be used.
|
|
132
|
+
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
133
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
|
|
134
|
+
* are derived booleans for convenience.
|
|
135
|
+
*
|
|
136
|
+
* @example
|
|
137
|
+
* ```tsx
|
|
138
|
+
* import { useQuery } from '@tanstack/react-query'
|
|
139
|
+
*
|
|
140
|
+
* function Posts() {
|
|
141
|
+
* const { status, data, error, isFetching } = useQuery({
|
|
142
|
+
* queryKey: ['posts'],
|
|
143
|
+
* queryFn: fetchPosts,
|
|
144
|
+
* })
|
|
145
|
+
*
|
|
146
|
+
* if (status === 'pending') return 'Loading...'
|
|
147
|
+
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
148
|
+
*
|
|
149
|
+
* return (
|
|
150
|
+
* <div>
|
|
151
|
+
* <ul>
|
|
152
|
+
* {data.map((post) => (
|
|
153
|
+
* <li key={post.id}>{post.title}</li>
|
|
154
|
+
* ))}
|
|
155
|
+
* </ul>
|
|
156
|
+
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
157
|
+
* </div>
|
|
158
|
+
* )
|
|
159
|
+
* }
|
|
160
|
+
* ```
|
|
161
|
+
*
|
|
162
|
+
* @example
|
|
163
|
+
* `select` derives whatever `data` a component needs from the cached value, without changing what's
|
|
164
|
+
* actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:
|
|
165
|
+
* ```tsx
|
|
166
|
+
* import { useQuery } from '@tanstack/react-query'
|
|
167
|
+
*
|
|
168
|
+
* function PostCount() {
|
|
169
|
+
* const { data, isPending, isError, error } = useQuery({
|
|
170
|
+
* queryKey: ['posts'],
|
|
171
|
+
* queryFn: fetchPosts,
|
|
172
|
+
* select: (posts) => posts.length,
|
|
173
|
+
* })
|
|
174
|
+
*
|
|
175
|
+
* if (isPending) return 'Loading...'
|
|
176
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
177
|
+
*
|
|
178
|
+
* return <span>{data} posts</span>
|
|
179
|
+
* }
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the
|
|
184
|
+
* loading state doesn't show while the query is disabled:
|
|
185
|
+
* ```tsx
|
|
186
|
+
* import { useQuery } from '@tanstack/react-query'
|
|
187
|
+
*
|
|
188
|
+
* function Post({ postId }: { postId: number | undefined }) {
|
|
189
|
+
* const { data, isLoading, isError, error } = useQuery({
|
|
190
|
+
* queryKey: ['post', postId],
|
|
191
|
+
* queryFn: () => fetchPost(postId!),
|
|
192
|
+
* enabled: postId != null,
|
|
193
|
+
* })
|
|
194
|
+
*
|
|
195
|
+
* if (postId == null) return 'Select a post'
|
|
196
|
+
* if (isLoading) return 'Loading...'
|
|
197
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
198
|
+
*
|
|
199
|
+
* return <h1>{data?.title}</h1>
|
|
200
|
+
* }
|
|
201
|
+
* ```
|
|
202
|
+
*
|
|
203
|
+
* @example
|
|
204
|
+
* The same dependent query, type safe: `skipToken` disables the query without needing the
|
|
205
|
+
* non-null assertion above, since `queryFn` is only ever called when `postId` is defined.
|
|
206
|
+
* `refetch` doesn't work while `queryFn` is `skipToken` — use `enabled: false` instead if you
|
|
207
|
+
* need to trigger the query manually:
|
|
208
|
+
* ```tsx
|
|
209
|
+
* import { skipToken, useQuery } from '@tanstack/react-query'
|
|
210
|
+
*
|
|
211
|
+
* function Post({ postId }: { postId: number | undefined }) {
|
|
212
|
+
* const { data, isLoading, isError, error } = useQuery({
|
|
213
|
+
* queryKey: ['post', postId],
|
|
214
|
+
* queryFn: postId != null ? () => fetchPost(postId) : skipToken,
|
|
215
|
+
* })
|
|
216
|
+
*
|
|
217
|
+
* if (postId == null) return 'Select a post'
|
|
218
|
+
* if (isLoading) return 'Loading...'
|
|
219
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
220
|
+
*
|
|
221
|
+
* return <h1>{data?.title}</h1>
|
|
222
|
+
* }
|
|
223
|
+
* ```
|
|
224
|
+
*
|
|
225
|
+
* @example
|
|
226
|
+
* Seeding a detail query from an already-cached list, to skip the loading state:
|
|
227
|
+
* ```tsx
|
|
228
|
+
* import { useQuery, useQueryClient } from '@tanstack/react-query'
|
|
229
|
+
*
|
|
230
|
+
* function Post({ postId }: { postId: number }) {
|
|
231
|
+
* const queryClient = useQueryClient()
|
|
232
|
+
*
|
|
233
|
+
* const { data, isError, error } = useQuery({
|
|
234
|
+
* queryKey: ['post', postId],
|
|
235
|
+
* queryFn: () => fetchPost(postId),
|
|
236
|
+
* initialData: () =>
|
|
237
|
+
* queryClient
|
|
238
|
+
* .getQueryData<Array<Post>>(['posts'])
|
|
239
|
+
* ?.find((post) => post.id === postId),
|
|
240
|
+
* })
|
|
241
|
+
*
|
|
242
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
243
|
+
*
|
|
244
|
+
* return <h1>{data?.title}</h1>
|
|
245
|
+
* }
|
|
246
|
+
* ```
|
|
247
|
+
*
|
|
248
|
+
* @example
|
|
249
|
+
* Paginated data, keeping the previous page's data visible while the next page loads:
|
|
250
|
+
* ```tsx
|
|
251
|
+
* import { keepPreviousData, useQuery } from '@tanstack/react-query'
|
|
252
|
+
* import { useState } from 'react'
|
|
253
|
+
*
|
|
254
|
+
* function Posts() {
|
|
255
|
+
* const [page, setPage] = useState(0)
|
|
256
|
+
*
|
|
257
|
+
* const { data, isPlaceholderData, isError, error } = useQuery({
|
|
258
|
+
* queryKey: ['posts', page],
|
|
259
|
+
* queryFn: () => fetchPosts(page),
|
|
260
|
+
* placeholderData: keepPreviousData,
|
|
261
|
+
* })
|
|
262
|
+
*
|
|
263
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
264
|
+
*
|
|
265
|
+
* return (
|
|
266
|
+
* <div>
|
|
267
|
+
* <ul>
|
|
268
|
+
* {data?.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
269
|
+
* </ul>
|
|
270
|
+
* <button
|
|
271
|
+
* disabled={isPlaceholderData}
|
|
272
|
+
* onClick={() => setPage((old) => old + 1)}
|
|
273
|
+
* >
|
|
274
|
+
* Next Page
|
|
275
|
+
* </button>
|
|
276
|
+
* </div>
|
|
277
|
+
* )
|
|
278
|
+
* }
|
|
279
|
+
* ```
|
|
280
|
+
*/
|
|
35
281
|
export function useQuery<
|
|
36
282
|
TQueryFnData = unknown,
|
|
37
283
|
TError = DefaultError,
|
|
@@ -15,6 +15,92 @@ import type {
|
|
|
15
15
|
UseSuspenseInfiniteQueryResult,
|
|
16
16
|
} from './types'
|
|
17
17
|
|
|
18
|
+
/**
|
|
19
|
+
* The options for `useSuspenseInfiniteQuery` are the same as for `useInfiniteQuery`, except for `throwOnError`,
|
|
20
|
+
* `enabled`, and `placeholderData`.
|
|
21
|
+
*
|
|
22
|
+
* Caveat: cancellation does not work.
|
|
23
|
+
*
|
|
24
|
+
* @remarks Multiple suspenseful query calls in the same component suspend serially, causing a request
|
|
25
|
+
* waterfall — each one blocks rendering until it resolves, so the next doesn't even start fetching until
|
|
26
|
+
* then. There's no way to parallelize multiple infinite queries under Suspense. Also keep in mind that
|
|
27
|
+
* imperative fetch calls, such as `fetchNextPage`, may interfere with the default refetch behavior,
|
|
28
|
+
* resulting in outdated data. Make sure to call these functions only in response to user actions, or add
|
|
29
|
+
* conditions like `hasNextPage && !isFetching`.
|
|
30
|
+
* @see {@link useInfiniteQuery} for the non-Suspense version of this hook.
|
|
31
|
+
* @param options - The {@link UseSuspenseInfiniteQueryOptions} to use — the same options as `useInfiniteQuery`, minus the ones listed above.
|
|
32
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
33
|
+
* be used.
|
|
34
|
+
* @returns The same object as `useInfiniteQuery`, except that `data` is guaranteed to be defined,
|
|
35
|
+
* `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived flags set
|
|
36
|
+
* accordingly).
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
|
|
40
|
+
* required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
|
|
41
|
+
* Use {@link QueryErrorResetBoundary} to let the user retry after such an error:
|
|
42
|
+
* ```tsx
|
|
43
|
+
* import { Suspense } from 'react'
|
|
44
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
45
|
+
* import {
|
|
46
|
+
* QueryErrorResetBoundary,
|
|
47
|
+
* useSuspenseInfiniteQuery,
|
|
48
|
+
* } from '@tanstack/react-query'
|
|
49
|
+
*
|
|
50
|
+
* function Projects() {
|
|
51
|
+
* // `data` is guaranteed to be defined here — no `isPending` check needed.
|
|
52
|
+
* const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
|
|
53
|
+
* useSuspenseInfiniteQuery({
|
|
54
|
+
* queryKey: ['projects'],
|
|
55
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
56
|
+
* initialPageParam: 0,
|
|
57
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
58
|
+
* })
|
|
59
|
+
*
|
|
60
|
+
* return (
|
|
61
|
+
* <div>
|
|
62
|
+
* <ul>
|
|
63
|
+
* {data.pages.map((page) =>
|
|
64
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
65
|
+
* )}
|
|
66
|
+
* </ul>
|
|
67
|
+
* <button
|
|
68
|
+
* onClick={() => fetchNextPage()}
|
|
69
|
+
* disabled={!hasNextPage || isFetching}
|
|
70
|
+
* >
|
|
71
|
+
* {isFetchingNextPage
|
|
72
|
+
* ? 'Loading more...'
|
|
73
|
+
* : hasNextPage
|
|
74
|
+
* ? 'Load More'
|
|
75
|
+
* : 'Nothing more to load'}
|
|
76
|
+
* </button>
|
|
77
|
+
* </div>
|
|
78
|
+
* )
|
|
79
|
+
* }
|
|
80
|
+
*
|
|
81
|
+
* function App() {
|
|
82
|
+
* return (
|
|
83
|
+
* <QueryErrorResetBoundary>
|
|
84
|
+
* {({ reset }) => (
|
|
85
|
+
* <ErrorBoundary
|
|
86
|
+
* onReset={reset}
|
|
87
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
88
|
+
* <div>
|
|
89
|
+
* There was an error!
|
|
90
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
91
|
+
* </div>
|
|
92
|
+
* )}
|
|
93
|
+
* >
|
|
94
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
95
|
+
* <Projects />
|
|
96
|
+
* </Suspense>
|
|
97
|
+
* </ErrorBoundary>
|
|
98
|
+
* )}
|
|
99
|
+
* </QueryErrorResetBoundary>
|
|
100
|
+
* )
|
|
101
|
+
* }
|
|
102
|
+
* ```
|
|
103
|
+
*/
|
|
18
104
|
export function useSuspenseInfiniteQuery<
|
|
19
105
|
TQueryFnData,
|
|
20
106
|
TError = DefaultError,
|