@tanstack/react-query 5.102.8 → 5.103.0
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
|
@@ -105,7 +105,16 @@ type GetUseSuspenseQueryResult<T> =
|
|
|
105
105
|
UseSuspenseQueryResult
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
*
|
|
108
|
+
* The `queries` array accepted by `useSuspenseQueries`. Recursively unwraps each tuple element so every
|
|
109
|
+
* entry's `queryFn`/`select` are inferred individually, up to 20 elements. An opaque array (e.g. `unknown[]`)
|
|
110
|
+
* is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls back to a
|
|
111
|
+
* single homogeneous {@link UseSuspenseQueryOptions} type.
|
|
112
|
+
*
|
|
113
|
+
* @template T - The type of the `queries` array as written at the call site.
|
|
114
|
+
* @template TResults - The internal accumulator that this type builds during recursion. It is not meant
|
|
115
|
+
* to be set explicitly.
|
|
116
|
+
* @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
|
|
117
|
+
* meant to be set explicitly.
|
|
109
118
|
*/
|
|
110
119
|
export type SuspenseQueriesOptions<
|
|
111
120
|
T extends Array<any>,
|
|
@@ -142,7 +151,16 @@ export type SuspenseQueriesOptions<
|
|
|
142
151
|
Array<UseSuspenseQueryOptions>
|
|
143
152
|
|
|
144
153
|
/**
|
|
145
|
-
*
|
|
154
|
+
* The result type returned by `useSuspenseQueries`, when no `combine` is provided. Mirrors
|
|
155
|
+
* {@link SuspenseQueriesOptions}: each tuple element's result type is inferred individually, up to 20 elements.
|
|
156
|
+
* A non-tuple array is mapped per-element instead, still inferring each entry individually; only past 20
|
|
157
|
+
* elements does this fall back to a single homogeneous {@link UseSuspenseQueryResult} type.
|
|
158
|
+
*
|
|
159
|
+
* @template T - The type of the `queries` array, as inferred by {@link SuspenseQueriesOptions}.
|
|
160
|
+
* @template TResults - The internal accumulator that this type builds during recursion. It is not meant
|
|
161
|
+
* to be set explicitly.
|
|
162
|
+
* @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
|
|
163
|
+
* meant to be set explicitly.
|
|
146
164
|
*/
|
|
147
165
|
export type SuspenseQueriesResults<
|
|
148
166
|
T extends Array<any>,
|
|
@@ -162,25 +180,325 @@ export type SuspenseQueriesResults<
|
|
|
162
180
|
>
|
|
163
181
|
: { [K in keyof T]: GetUseSuspenseQueryResult<T[K]> }
|
|
164
182
|
|
|
183
|
+
/**
|
|
184
|
+
* The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed`
|
|
185
|
+
* option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
|
|
186
|
+
*
|
|
187
|
+
* @param options - The `queries` array to run in Suspense, and an optional `combine` function.
|
|
188
|
+
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
189
|
+
* will be used.
|
|
190
|
+
* @returns The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be
|
|
191
|
+
* defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived
|
|
192
|
+
* flags set accordingly).
|
|
193
|
+
*
|
|
194
|
+
* Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone
|
|
195
|
+
* stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid
|
|
196
|
+
* this, make sure to set a high enough `staleTime`. Cancellation does not work.
|
|
197
|
+
*
|
|
198
|
+
* @example
|
|
199
|
+
* The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
|
|
200
|
+
* required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
|
|
201
|
+
* Use {@link QueryErrorResetBoundary} to let the user retry after such an error:
|
|
202
|
+
* ```tsx
|
|
203
|
+
* import { Suspense } from 'react'
|
|
204
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
205
|
+
* import {
|
|
206
|
+
* QueryErrorResetBoundary,
|
|
207
|
+
* useSuspenseQueries,
|
|
208
|
+
* } from '@tanstack/react-query'
|
|
209
|
+
*
|
|
210
|
+
* function Posts({ ids }: { ids: Array<number> }) {
|
|
211
|
+
* // Every result is guaranteed to be defined — no per-query `isPending` check needed.
|
|
212
|
+
* const postQueries = useSuspenseQueries({
|
|
213
|
+
* queries: ids.map((id) => ({
|
|
214
|
+
* queryKey: ['post', id],
|
|
215
|
+
* queryFn: () => fetchPost(id),
|
|
216
|
+
* })),
|
|
217
|
+
* })
|
|
218
|
+
*
|
|
219
|
+
* return (
|
|
220
|
+
* <ul>
|
|
221
|
+
* {postQueries.map((query) => (
|
|
222
|
+
* <li key={query.data.id}>{query.data.title}</li>
|
|
223
|
+
* ))}
|
|
224
|
+
* </ul>
|
|
225
|
+
* )
|
|
226
|
+
* }
|
|
227
|
+
*
|
|
228
|
+
* function App() {
|
|
229
|
+
* return (
|
|
230
|
+
* <QueryErrorResetBoundary>
|
|
231
|
+
* {({ reset }) => (
|
|
232
|
+
* <ErrorBoundary
|
|
233
|
+
* onReset={reset}
|
|
234
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
235
|
+
* <div>
|
|
236
|
+
* There was an error!
|
|
237
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
238
|
+
* </div>
|
|
239
|
+
* )}
|
|
240
|
+
* >
|
|
241
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
242
|
+
* <Posts ids={[1, 2, 3]} />
|
|
243
|
+
* </Suspense>
|
|
244
|
+
* </ErrorBoundary>
|
|
245
|
+
* )}
|
|
246
|
+
* </QueryErrorResetBoundary>
|
|
247
|
+
* )
|
|
248
|
+
* }
|
|
249
|
+
* ```
|
|
250
|
+
*
|
|
251
|
+
* @example
|
|
252
|
+
* Several different queries — use `useSuspenseQueries` instead of multiple `useSuspenseQuery` calls, so
|
|
253
|
+
* they fetch in parallel rather than suspending one after another:
|
|
254
|
+
* ```tsx
|
|
255
|
+
* import { Suspense } from 'react'
|
|
256
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
257
|
+
* import {
|
|
258
|
+
* QueryErrorResetBoundary,
|
|
259
|
+
* useSuspenseQueries,
|
|
260
|
+
* } from '@tanstack/react-query'
|
|
261
|
+
*
|
|
262
|
+
* function Dashboard() {
|
|
263
|
+
* const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({
|
|
264
|
+
* queries: [
|
|
265
|
+
* { queryKey: ['users'], queryFn: fetchUsers },
|
|
266
|
+
* { queryKey: ['teams'], queryFn: fetchTeams },
|
|
267
|
+
* { queryKey: ['projects'], queryFn: fetchProjects },
|
|
268
|
+
* ],
|
|
269
|
+
* })
|
|
270
|
+
*
|
|
271
|
+
* return (
|
|
272
|
+
* <div>
|
|
273
|
+
* <UserList users={usersQuery.data} />
|
|
274
|
+
* <TeamList teams={teamsQuery.data} />
|
|
275
|
+
* <ProjectList projects={projectsQuery.data} />
|
|
276
|
+
* </div>
|
|
277
|
+
* )
|
|
278
|
+
* }
|
|
279
|
+
*
|
|
280
|
+
* function App() {
|
|
281
|
+
* return (
|
|
282
|
+
* <QueryErrorResetBoundary>
|
|
283
|
+
* {({ reset }) => (
|
|
284
|
+
* <ErrorBoundary
|
|
285
|
+
* onReset={reset}
|
|
286
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
287
|
+
* <div>
|
|
288
|
+
* There was an error!
|
|
289
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
290
|
+
* </div>
|
|
291
|
+
* )}
|
|
292
|
+
* >
|
|
293
|
+
* <Suspense fallback={<h1>Loading dashboard...</h1>}>
|
|
294
|
+
* <Dashboard />
|
|
295
|
+
* </Suspense>
|
|
296
|
+
* </ErrorBoundary>
|
|
297
|
+
* )}
|
|
298
|
+
* </QueryErrorResetBoundary>
|
|
299
|
+
* )
|
|
300
|
+
* }
|
|
301
|
+
* ```
|
|
302
|
+
*
|
|
303
|
+
* @example
|
|
304
|
+
* `combine`s the results into a single boolean, so `Refresh` only re-renders when that boolean changes,
|
|
305
|
+
* not on every individual query update. This overload is the only one that accepts `combine`:
|
|
306
|
+
* ```tsx
|
|
307
|
+
* import { Suspense } from 'react'
|
|
308
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
309
|
+
* import {
|
|
310
|
+
* QueryErrorResetBoundary,
|
|
311
|
+
* useSuspenseQueries,
|
|
312
|
+
* } from '@tanstack/react-query'
|
|
313
|
+
*
|
|
314
|
+
* function Refresh() {
|
|
315
|
+
* const anyFetching = useSuspenseQueries({
|
|
316
|
+
* queries: [
|
|
317
|
+
* { queryKey: ['users'], queryFn: fetchUsers },
|
|
318
|
+
* { queryKey: ['teams'], queryFn: fetchTeams },
|
|
319
|
+
* ],
|
|
320
|
+
* combine: (results) => results.some((result) => result.isFetching),
|
|
321
|
+
* })
|
|
322
|
+
*
|
|
323
|
+
* return anyFetching ? <span>Refreshing…</span> : null
|
|
324
|
+
* }
|
|
325
|
+
*
|
|
326
|
+
* function App() {
|
|
327
|
+
* return (
|
|
328
|
+
* <QueryErrorResetBoundary>
|
|
329
|
+
* {({ reset }) => (
|
|
330
|
+
* <ErrorBoundary
|
|
331
|
+
* onReset={reset}
|
|
332
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
333
|
+
* <div>
|
|
334
|
+
* There was an error!
|
|
335
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
336
|
+
* </div>
|
|
337
|
+
* )}
|
|
338
|
+
* >
|
|
339
|
+
* <Suspense fallback={<h1>Loading dashboard...</h1>}>
|
|
340
|
+
* <Refresh />
|
|
341
|
+
* </Suspense>
|
|
342
|
+
* </ErrorBoundary>
|
|
343
|
+
* )}
|
|
344
|
+
* </QueryErrorResetBoundary>
|
|
345
|
+
* )
|
|
346
|
+
* }
|
|
347
|
+
* ```
|
|
348
|
+
*/
|
|
165
349
|
export function useSuspenseQueries<
|
|
166
350
|
T extends Array<any>,
|
|
167
351
|
TCombinedResult = SuspenseQueriesResults<T>,
|
|
168
352
|
>(
|
|
169
353
|
options: {
|
|
354
|
+
/**
|
|
355
|
+
* An array with query option objects identical to `useSuspenseQuery`.
|
|
356
|
+
*/
|
|
170
357
|
queries:
|
|
171
358
|
| readonly [...SuspenseQueriesOptions<T>]
|
|
172
359
|
| readonly [...{ [K in keyof T]: GetUseSuspenseQueryOptions<T[K]> }]
|
|
360
|
+
/**
|
|
361
|
+
* Use this to combine the results of the queries into a single value. The result will be structurally
|
|
362
|
+
* shared to be as referentially stable as possible.
|
|
363
|
+
*/
|
|
173
364
|
combine?: (result: SuspenseQueriesResults<T>) => TCombinedResult
|
|
174
365
|
},
|
|
175
366
|
queryClient?: QueryClient,
|
|
176
367
|
): TCombinedResult
|
|
177
368
|
|
|
369
|
+
/**
|
|
370
|
+
* The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed`
|
|
371
|
+
* option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
|
|
372
|
+
*
|
|
373
|
+
* @param options - The `queries` array to run in Suspense, and an optional `combine` function.
|
|
374
|
+
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
375
|
+
* will be used.
|
|
376
|
+
* @returns The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be
|
|
377
|
+
* defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived
|
|
378
|
+
* flags set accordingly).
|
|
379
|
+
*
|
|
380
|
+
* Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone
|
|
381
|
+
* stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid
|
|
382
|
+
* this, make sure to set a high enough `staleTime`. Cancellation does not work.
|
|
383
|
+
*
|
|
384
|
+
* @example
|
|
385
|
+
* The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
|
|
386
|
+
* required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
|
|
387
|
+
* Use {@link QueryErrorResetBoundary} to let the user retry after such an error:
|
|
388
|
+
* ```tsx
|
|
389
|
+
* import { Suspense } from 'react'
|
|
390
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
391
|
+
* import {
|
|
392
|
+
* QueryErrorResetBoundary,
|
|
393
|
+
* useSuspenseQueries,
|
|
394
|
+
* } from '@tanstack/react-query'
|
|
395
|
+
*
|
|
396
|
+
* function Posts({ ids }: { ids: Array<number> }) {
|
|
397
|
+
* // Every result is guaranteed to be defined — no per-query `isPending` check needed.
|
|
398
|
+
* const postQueries = useSuspenseQueries({
|
|
399
|
+
* queries: ids.map((id) => ({
|
|
400
|
+
* queryKey: ['post', id],
|
|
401
|
+
* queryFn: () => fetchPost(id),
|
|
402
|
+
* })),
|
|
403
|
+
* })
|
|
404
|
+
*
|
|
405
|
+
* return (
|
|
406
|
+
* <ul>
|
|
407
|
+
* {postQueries.map((query) => (
|
|
408
|
+
* <li key={query.data.id}>{query.data.title}</li>
|
|
409
|
+
* ))}
|
|
410
|
+
* </ul>
|
|
411
|
+
* )
|
|
412
|
+
* }
|
|
413
|
+
*
|
|
414
|
+
* function App() {
|
|
415
|
+
* return (
|
|
416
|
+
* <QueryErrorResetBoundary>
|
|
417
|
+
* {({ reset }) => (
|
|
418
|
+
* <ErrorBoundary
|
|
419
|
+
* onReset={reset}
|
|
420
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
421
|
+
* <div>
|
|
422
|
+
* There was an error!
|
|
423
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
424
|
+
* </div>
|
|
425
|
+
* )}
|
|
426
|
+
* >
|
|
427
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
428
|
+
* <Posts ids={[1, 2, 3]} />
|
|
429
|
+
* </Suspense>
|
|
430
|
+
* </ErrorBoundary>
|
|
431
|
+
* )}
|
|
432
|
+
* </QueryErrorResetBoundary>
|
|
433
|
+
* )
|
|
434
|
+
* }
|
|
435
|
+
* ```
|
|
436
|
+
*
|
|
437
|
+
* @example
|
|
438
|
+
* Several different queries — use `useSuspenseQueries` instead of multiple `useSuspenseQuery` calls, so
|
|
439
|
+
* they fetch in parallel rather than suspending one after another:
|
|
440
|
+
* ```tsx
|
|
441
|
+
* import { Suspense } from 'react'
|
|
442
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
443
|
+
* import {
|
|
444
|
+
* QueryErrorResetBoundary,
|
|
445
|
+
* useSuspenseQueries,
|
|
446
|
+
* } from '@tanstack/react-query'
|
|
447
|
+
*
|
|
448
|
+
* function Dashboard() {
|
|
449
|
+
* const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({
|
|
450
|
+
* queries: [
|
|
451
|
+
* { queryKey: ['users'], queryFn: fetchUsers },
|
|
452
|
+
* { queryKey: ['teams'], queryFn: fetchTeams },
|
|
453
|
+
* { queryKey: ['projects'], queryFn: fetchProjects },
|
|
454
|
+
* ],
|
|
455
|
+
* })
|
|
456
|
+
*
|
|
457
|
+
* return (
|
|
458
|
+
* <div>
|
|
459
|
+
* <UserList users={usersQuery.data} />
|
|
460
|
+
* <TeamList teams={teamsQuery.data} />
|
|
461
|
+
* <ProjectList projects={projectsQuery.data} />
|
|
462
|
+
* </div>
|
|
463
|
+
* )
|
|
464
|
+
* }
|
|
465
|
+
*
|
|
466
|
+
* function App() {
|
|
467
|
+
* return (
|
|
468
|
+
* <QueryErrorResetBoundary>
|
|
469
|
+
* {({ reset }) => (
|
|
470
|
+
* <ErrorBoundary
|
|
471
|
+
* onReset={reset}
|
|
472
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
473
|
+
* <div>
|
|
474
|
+
* There was an error!
|
|
475
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
476
|
+
* </div>
|
|
477
|
+
* )}
|
|
478
|
+
* >
|
|
479
|
+
* <Suspense fallback={<h1>Loading dashboard...</h1>}>
|
|
480
|
+
* <Dashboard />
|
|
481
|
+
* </Suspense>
|
|
482
|
+
* </ErrorBoundary>
|
|
483
|
+
* )}
|
|
484
|
+
* </QueryErrorResetBoundary>
|
|
485
|
+
* )
|
|
486
|
+
* }
|
|
487
|
+
* ```
|
|
488
|
+
*/
|
|
178
489
|
export function useSuspenseQueries<
|
|
179
490
|
T extends Array<any>,
|
|
180
491
|
TCombinedResult = SuspenseQueriesResults<T>,
|
|
181
492
|
>(
|
|
182
493
|
options: {
|
|
494
|
+
/**
|
|
495
|
+
* An array with query option objects identical to `useSuspenseQuery`.
|
|
496
|
+
*/
|
|
183
497
|
queries: readonly [...SuspenseQueriesOptions<T>]
|
|
498
|
+
/**
|
|
499
|
+
* Use this to combine the results of the queries into a single value. The result will be structurally
|
|
500
|
+
* shared to be as referentially stable as possible.
|
|
501
|
+
*/
|
|
184
502
|
combine?: (result: SuspenseQueriesResults<T>) => TCombinedResult
|
|
185
503
|
},
|
|
186
504
|
queryClient?: QueryClient,
|
package/src/useSuspenseQuery.ts
CHANGED
|
@@ -5,6 +5,73 @@ import { defaultThrowOnError } from './suspense'
|
|
|
5
5
|
import type { UseSuspenseQueryOptions, UseSuspenseQueryResult } from './types'
|
|
6
6
|
import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* The options for `useSuspenseQuery` are the same as for `useQuery`, except for `throwOnError`, `enabled`, and
|
|
10
|
+
* `placeholderData` — and `queryFn` may not be `skipToken`, since Suspense hooks can't render a "disabled" state.
|
|
11
|
+
*
|
|
12
|
+
* Caveat: cancellation does not work.
|
|
13
|
+
*
|
|
14
|
+
* @remarks Multiple `useSuspenseQuery` calls in the same component suspend serially, causing a request
|
|
15
|
+
* waterfall — each one blocks rendering until it resolves, so the next doesn't even start fetching until then.
|
|
16
|
+
* Use {@link useSuspenseQueries} instead when you have more than one suspenseful query in a component, so they
|
|
17
|
+
* fetch in parallel.
|
|
18
|
+
* @param options - The {@link UseSuspenseQueryOptions} to use — the same options as `useQuery`, minus the ones listed above.
|
|
19
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
20
|
+
* be used.
|
|
21
|
+
* @returns The same object as `useQuery`, except that `data` is guaranteed to be defined, `isPlaceholderData`
|
|
22
|
+
* is missing, and `status` is either `success` or `error` (with the derived flags set accordingly).
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* The query error is thrown if the fetch fails and no cached data exists yet, so an error boundary is
|
|
26
|
+
* required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
|
|
27
|
+
* Use {@link QueryErrorResetBoundary} to let the user retry after such an error:
|
|
28
|
+
* ```tsx
|
|
29
|
+
* import { Suspense } from 'react'
|
|
30
|
+
* import { ErrorBoundary } from 'react-error-boundary'
|
|
31
|
+
* import { QueryErrorResetBoundary, useSuspenseQuery } from '@tanstack/react-query'
|
|
32
|
+
*
|
|
33
|
+
* function Posts() {
|
|
34
|
+
* // `data` is guaranteed to be defined here — no `isPending` check needed.
|
|
35
|
+
* const { data, isFetching } = useSuspenseQuery({
|
|
36
|
+
* queryKey: ['posts'],
|
|
37
|
+
* queryFn: fetchPosts,
|
|
38
|
+
* })
|
|
39
|
+
*
|
|
40
|
+
* return (
|
|
41
|
+
* <div>
|
|
42
|
+
* <h1>Posts {isFetching ? '(refreshing...)' : null}</h1>
|
|
43
|
+
* <ul>
|
|
44
|
+
* {data.map((post) => (
|
|
45
|
+
* <li key={post.id}>{post.title}</li>
|
|
46
|
+
* ))}
|
|
47
|
+
* </ul>
|
|
48
|
+
* </div>
|
|
49
|
+
* )
|
|
50
|
+
* }
|
|
51
|
+
*
|
|
52
|
+
* function App() {
|
|
53
|
+
* return (
|
|
54
|
+
* <QueryErrorResetBoundary>
|
|
55
|
+
* {({ reset }) => (
|
|
56
|
+
* <ErrorBoundary
|
|
57
|
+
* onReset={reset}
|
|
58
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
59
|
+
* <div>
|
|
60
|
+
* There was an error!
|
|
61
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
62
|
+
* </div>
|
|
63
|
+
* )}
|
|
64
|
+
* >
|
|
65
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
66
|
+
* <Posts />
|
|
67
|
+
* </Suspense>
|
|
68
|
+
* </ErrorBoundary>
|
|
69
|
+
* )}
|
|
70
|
+
* </QueryErrorResetBoundary>
|
|
71
|
+
* )
|
|
72
|
+
* }
|
|
73
|
+
* ```
|
|
74
|
+
*/
|
|
8
75
|
export function useSuspenseQuery<
|
|
9
76
|
TQueryFnData = unknown,
|
|
10
77
|
TError = DefaultError,
|