@tanstack/preact-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 +1 -1
- package/build/legacy/HydrationBoundary.cjs.map +1 -1
- package/build/legacy/HydrationBoundary.js +1 -1
- package/build/legacy/HydrationBoundary.js.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.cjs +12 -12
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +12 -12
- package/build/legacy/QueryErrorResetBoundary.d.ts +12 -12
- package/build/legacy/QueryErrorResetBoundary.js +12 -12
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +41 -55
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +41 -55
- 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 +4 -17
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +4 -17
- 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 +51 -74
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +51 -74
- package/build/legacy/queryOptions.d.ts.map +1 -1
- package/build/legacy/queryOptions.js.map +1 -1
- package/build/legacy/useInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useInfiniteQuery.d.cts +208 -36
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +208 -36
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +8 -6
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +8 -6
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +8 -6
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +8 -6
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +13 -11
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +13 -11
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +13 -11
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +13 -11
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +49 -29
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +49 -29
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +49 -29
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +49 -29
- package/build/legacy/useMutationState.js.map +1 -1
- package/build/legacy/useQueries.cjs +50 -23
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +50 -23
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +50 -23
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +50 -23
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +93 -33
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +93 -33
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +57 -7
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +244 -20
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +244 -20
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +48 -8
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +48 -8
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +48 -8
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +48 -8
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +1 -1
- package/build/modern/HydrationBoundary.cjs.map +1 -1
- package/build/modern/HydrationBoundary.js +1 -1
- package/build/modern/HydrationBoundary.js.map +1 -1
- package/build/modern/QueryErrorResetBoundary.cjs +12 -12
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +12 -12
- package/build/modern/QueryErrorResetBoundary.d.ts +12 -12
- package/build/modern/QueryErrorResetBoundary.js +12 -12
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +41 -55
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +41 -55
- 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 +4 -17
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +4 -17
- 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 +51 -74
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +51 -74
- package/build/modern/queryOptions.d.ts.map +1 -1
- package/build/modern/queryOptions.js.map +1 -1
- package/build/modern/useInfiniteQuery.cjs.map +1 -1
- package/build/modern/useInfiniteQuery.d.cts +208 -36
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +208 -36
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +8 -6
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +8 -6
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +8 -6
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +8 -6
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +13 -11
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +13 -11
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +13 -11
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +13 -11
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +49 -29
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +49 -29
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +49 -29
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +49 -29
- package/build/modern/useMutationState.js.map +1 -1
- package/build/modern/useQueries.cjs +50 -23
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +50 -23
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +50 -23
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +50 -23
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +93 -33
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +93 -33
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +57 -7
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +57 -7
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +57 -7
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +57 -7
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +244 -20
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +244 -20
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +48 -8
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +48 -8
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +48 -8
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +48 -8
- 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 +0 -1
- package/src/QueryErrorResetBoundary.tsx +12 -12
- package/src/infiniteQueryOptions.ts +41 -55
- package/src/mutationOptions.ts +4 -17
- package/src/queryOptions.ts +50 -73
- package/src/useInfiniteQuery.ts +208 -36
- package/src/useIsFetching.ts +8 -6
- package/src/useMutation.ts +13 -11
- package/src/useMutationState.ts +49 -29
- package/src/useQueries.ts +50 -23
- package/src/useQuery.ts +93 -33
- package/src/useSuspenseInfiniteQuery.ts +57 -7
- package/src/useSuspenseQueries.ts +244 -20
- package/src/useSuspenseQuery.ts +48 -8
|
@@ -181,8 +181,8 @@ export type SuspenseQueriesResults<
|
|
|
181
181
|
: { [K in keyof T]: GetUseSuspenseQueryResult<T[K]> }
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
|
-
* The options for `useSuspenseQueries` are the same as for `useQueries`, except that
|
|
185
|
-
* `throwOnError`, `enabled`, or `placeholderData`.
|
|
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
186
|
*
|
|
187
187
|
* @param options - The `queries` array to run in Suspense, and an optional `combine` function.
|
|
188
188
|
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
@@ -196,9 +196,17 @@ export type SuspenseQueriesResults<
|
|
|
196
196
|
* this, make sure to set a high enough `staleTime`. Cancellation does not work.
|
|
197
197
|
*
|
|
198
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:
|
|
199
202
|
* ```tsx
|
|
200
203
|
* import { Suspense } from 'preact/compat'
|
|
201
|
-
* import {
|
|
204
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
205
|
+
* import {
|
|
206
|
+
* QueryErrorResetBoundary,
|
|
207
|
+
* useSuspenseQueries,
|
|
208
|
+
* } from '@tanstack/preact-query'
|
|
209
|
+
* import type { ComponentChildren } from 'preact'
|
|
202
210
|
*
|
|
203
211
|
* function Posts({ ids }: { ids: Array<number> }) {
|
|
204
212
|
* // Every result is guaranteed to be defined — no per-query `isPending` check needed.
|
|
@@ -220,11 +228,44 @@ export type SuspenseQueriesResults<
|
|
|
220
228
|
*
|
|
221
229
|
* function App() {
|
|
222
230
|
* return (
|
|
223
|
-
* <
|
|
224
|
-
*
|
|
225
|
-
*
|
|
231
|
+
* <QueryErrorResetBoundary>
|
|
232
|
+
* {({ reset }) => (
|
|
233
|
+
* <ErrorBoundary
|
|
234
|
+
* onReset={reset}
|
|
235
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
236
|
+
* <div>
|
|
237
|
+
* There was an error!
|
|
238
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
239
|
+
* </div>
|
|
240
|
+
* )}
|
|
241
|
+
* >
|
|
242
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
243
|
+
* <Posts ids={[1, 2, 3]} />
|
|
244
|
+
* </Suspense>
|
|
245
|
+
* </ErrorBoundary>
|
|
246
|
+
* )}
|
|
247
|
+
* </QueryErrorResetBoundary>
|
|
226
248
|
* )
|
|
227
249
|
* }
|
|
250
|
+
*
|
|
251
|
+
* function ErrorBoundary({
|
|
252
|
+
* children,
|
|
253
|
+
* onReset,
|
|
254
|
+
* fallbackRender,
|
|
255
|
+
* }: {
|
|
256
|
+
* children: ComponentChildren
|
|
257
|
+
* onReset: () => void
|
|
258
|
+
* fallbackRender: (props: {
|
|
259
|
+
* error: Error
|
|
260
|
+
* resetErrorBoundary: () => void
|
|
261
|
+
* }) => ComponentChildren
|
|
262
|
+
* }) {
|
|
263
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
264
|
+
*
|
|
265
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
266
|
+
*
|
|
267
|
+
* return children
|
|
268
|
+
* }
|
|
228
269
|
* ```
|
|
229
270
|
*
|
|
230
271
|
* @example
|
|
@@ -232,7 +273,12 @@ export type SuspenseQueriesResults<
|
|
|
232
273
|
* they fetch in parallel rather than suspending one after another:
|
|
233
274
|
* ```tsx
|
|
234
275
|
* import { Suspense } from 'preact/compat'
|
|
235
|
-
* import {
|
|
276
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
277
|
+
* import {
|
|
278
|
+
* QueryErrorResetBoundary,
|
|
279
|
+
* useSuspenseQueries,
|
|
280
|
+
* } from '@tanstack/preact-query'
|
|
281
|
+
* import type { ComponentChildren } from 'preact'
|
|
236
282
|
*
|
|
237
283
|
* function Dashboard() {
|
|
238
284
|
* const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({
|
|
@@ -254,11 +300,110 @@ export type SuspenseQueriesResults<
|
|
|
254
300
|
*
|
|
255
301
|
* function App() {
|
|
256
302
|
* return (
|
|
257
|
-
* <
|
|
258
|
-
*
|
|
259
|
-
*
|
|
303
|
+
* <QueryErrorResetBoundary>
|
|
304
|
+
* {({ reset }) => (
|
|
305
|
+
* <ErrorBoundary
|
|
306
|
+
* onReset={reset}
|
|
307
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
308
|
+
* <div>
|
|
309
|
+
* There was an error!
|
|
310
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
311
|
+
* </div>
|
|
312
|
+
* )}
|
|
313
|
+
* >
|
|
314
|
+
* <Suspense fallback={<h1>Loading dashboard...</h1>}>
|
|
315
|
+
* <Dashboard />
|
|
316
|
+
* </Suspense>
|
|
317
|
+
* </ErrorBoundary>
|
|
318
|
+
* )}
|
|
319
|
+
* </QueryErrorResetBoundary>
|
|
320
|
+
* )
|
|
321
|
+
* }
|
|
322
|
+
*
|
|
323
|
+
* function ErrorBoundary({
|
|
324
|
+
* children,
|
|
325
|
+
* onReset,
|
|
326
|
+
* fallbackRender,
|
|
327
|
+
* }: {
|
|
328
|
+
* children: ComponentChildren
|
|
329
|
+
* onReset: () => void
|
|
330
|
+
* fallbackRender: (props: {
|
|
331
|
+
* error: Error
|
|
332
|
+
* resetErrorBoundary: () => void
|
|
333
|
+
* }) => ComponentChildren
|
|
334
|
+
* }) {
|
|
335
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
336
|
+
*
|
|
337
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
338
|
+
*
|
|
339
|
+
* return children
|
|
340
|
+
* }
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @example
|
|
344
|
+
* `combine`s the results into a single boolean, so `Refresh` only re-renders when that boolean changes,
|
|
345
|
+
* not on every individual query update. This overload is the only one that accepts `combine`:
|
|
346
|
+
* ```tsx
|
|
347
|
+
* import { Suspense } from 'preact/compat'
|
|
348
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
349
|
+
* import {
|
|
350
|
+
* QueryErrorResetBoundary,
|
|
351
|
+
* useSuspenseQueries,
|
|
352
|
+
* } from '@tanstack/preact-query'
|
|
353
|
+
* import type { ComponentChildren } from 'preact'
|
|
354
|
+
*
|
|
355
|
+
* function Refresh() {
|
|
356
|
+
* const anyFetching = useSuspenseQueries({
|
|
357
|
+
* queries: [
|
|
358
|
+
* { queryKey: ['users'], queryFn: fetchUsers },
|
|
359
|
+
* { queryKey: ['teams'], queryFn: fetchTeams },
|
|
360
|
+
* ],
|
|
361
|
+
* combine: (results) => results.some((result) => result.isFetching),
|
|
362
|
+
* })
|
|
363
|
+
*
|
|
364
|
+
* return anyFetching ? <span>Refreshing…</span> : null
|
|
365
|
+
* }
|
|
366
|
+
*
|
|
367
|
+
* function App() {
|
|
368
|
+
* return (
|
|
369
|
+
* <QueryErrorResetBoundary>
|
|
370
|
+
* {({ reset }) => (
|
|
371
|
+
* <ErrorBoundary
|
|
372
|
+
* onReset={reset}
|
|
373
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
374
|
+
* <div>
|
|
375
|
+
* There was an error!
|
|
376
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
377
|
+
* </div>
|
|
378
|
+
* )}
|
|
379
|
+
* >
|
|
380
|
+
* <Suspense fallback={<h1>Loading dashboard...</h1>}>
|
|
381
|
+
* <Refresh />
|
|
382
|
+
* </Suspense>
|
|
383
|
+
* </ErrorBoundary>
|
|
384
|
+
* )}
|
|
385
|
+
* </QueryErrorResetBoundary>
|
|
260
386
|
* )
|
|
261
387
|
* }
|
|
388
|
+
*
|
|
389
|
+
* function ErrorBoundary({
|
|
390
|
+
* children,
|
|
391
|
+
* onReset,
|
|
392
|
+
* fallbackRender,
|
|
393
|
+
* }: {
|
|
394
|
+
* children: ComponentChildren
|
|
395
|
+
* onReset: () => void
|
|
396
|
+
* fallbackRender: (props: {
|
|
397
|
+
* error: Error
|
|
398
|
+
* resetErrorBoundary: () => void
|
|
399
|
+
* }) => ComponentChildren
|
|
400
|
+
* }) {
|
|
401
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
402
|
+
*
|
|
403
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
404
|
+
*
|
|
405
|
+
* return children
|
|
406
|
+
* }
|
|
262
407
|
* ```
|
|
263
408
|
*/
|
|
264
409
|
export function useSuspenseQueries<
|
|
@@ -282,8 +427,8 @@ export function useSuspenseQueries<
|
|
|
282
427
|
): TCombinedResult
|
|
283
428
|
|
|
284
429
|
/**
|
|
285
|
-
* The options for `useSuspenseQueries` are the same as for `useQueries`, except that
|
|
286
|
-
* `throwOnError`, `enabled`, or `placeholderData`.
|
|
430
|
+
* The options for `useSuspenseQueries` are the same as for `useQueries`, except that the top-level `subscribed`
|
|
431
|
+
* option isn't supported, and each `query` can't have `throwOnError`, `enabled`, or `placeholderData`.
|
|
287
432
|
*
|
|
288
433
|
* @param options - The `queries` array to run in Suspense, and an optional `combine` function.
|
|
289
434
|
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
@@ -297,9 +442,17 @@ export function useSuspenseQueries<
|
|
|
297
442
|
* this, make sure to set a high enough `staleTime`. Cancellation does not work.
|
|
298
443
|
*
|
|
299
444
|
* @example
|
|
445
|
+
* The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
|
|
446
|
+
* required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
|
|
447
|
+
* Use {@link QueryErrorResetBoundary} to let the user retry after such an error:
|
|
300
448
|
* ```tsx
|
|
301
449
|
* import { Suspense } from 'preact/compat'
|
|
302
|
-
* import {
|
|
450
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
451
|
+
* import {
|
|
452
|
+
* QueryErrorResetBoundary,
|
|
453
|
+
* useSuspenseQueries,
|
|
454
|
+
* } from '@tanstack/preact-query'
|
|
455
|
+
* import type { ComponentChildren } from 'preact'
|
|
303
456
|
*
|
|
304
457
|
* function Posts({ ids }: { ids: Array<number> }) {
|
|
305
458
|
* // Every result is guaranteed to be defined — no per-query `isPending` check needed.
|
|
@@ -321,11 +474,44 @@ export function useSuspenseQueries<
|
|
|
321
474
|
*
|
|
322
475
|
* function App() {
|
|
323
476
|
* return (
|
|
324
|
-
* <
|
|
325
|
-
*
|
|
326
|
-
*
|
|
477
|
+
* <QueryErrorResetBoundary>
|
|
478
|
+
* {({ reset }) => (
|
|
479
|
+
* <ErrorBoundary
|
|
480
|
+
* onReset={reset}
|
|
481
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
482
|
+
* <div>
|
|
483
|
+
* There was an error!
|
|
484
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
485
|
+
* </div>
|
|
486
|
+
* )}
|
|
487
|
+
* >
|
|
488
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
489
|
+
* <Posts ids={[1, 2, 3]} />
|
|
490
|
+
* </Suspense>
|
|
491
|
+
* </ErrorBoundary>
|
|
492
|
+
* )}
|
|
493
|
+
* </QueryErrorResetBoundary>
|
|
327
494
|
* )
|
|
328
495
|
* }
|
|
496
|
+
*
|
|
497
|
+
* function ErrorBoundary({
|
|
498
|
+
* children,
|
|
499
|
+
* onReset,
|
|
500
|
+
* fallbackRender,
|
|
501
|
+
* }: {
|
|
502
|
+
* children: ComponentChildren
|
|
503
|
+
* onReset: () => void
|
|
504
|
+
* fallbackRender: (props: {
|
|
505
|
+
* error: Error
|
|
506
|
+
* resetErrorBoundary: () => void
|
|
507
|
+
* }) => ComponentChildren
|
|
508
|
+
* }) {
|
|
509
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
510
|
+
*
|
|
511
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
512
|
+
*
|
|
513
|
+
* return children
|
|
514
|
+
* }
|
|
329
515
|
* ```
|
|
330
516
|
*
|
|
331
517
|
* @example
|
|
@@ -333,7 +519,12 @@ export function useSuspenseQueries<
|
|
|
333
519
|
* they fetch in parallel rather than suspending one after another:
|
|
334
520
|
* ```tsx
|
|
335
521
|
* import { Suspense } from 'preact/compat'
|
|
336
|
-
* import {
|
|
522
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
523
|
+
* import {
|
|
524
|
+
* QueryErrorResetBoundary,
|
|
525
|
+
* useSuspenseQueries,
|
|
526
|
+
* } from '@tanstack/preact-query'
|
|
527
|
+
* import type { ComponentChildren } from 'preact'
|
|
337
528
|
*
|
|
338
529
|
* function Dashboard() {
|
|
339
530
|
* const [usersQuery, teamsQuery, projectsQuery] = useSuspenseQueries({
|
|
@@ -355,11 +546,44 @@ export function useSuspenseQueries<
|
|
|
355
546
|
*
|
|
356
547
|
* function App() {
|
|
357
548
|
* return (
|
|
358
|
-
* <
|
|
359
|
-
*
|
|
360
|
-
*
|
|
549
|
+
* <QueryErrorResetBoundary>
|
|
550
|
+
* {({ reset }) => (
|
|
551
|
+
* <ErrorBoundary
|
|
552
|
+
* onReset={reset}
|
|
553
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
554
|
+
* <div>
|
|
555
|
+
* There was an error!
|
|
556
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
557
|
+
* </div>
|
|
558
|
+
* )}
|
|
559
|
+
* >
|
|
560
|
+
* <Suspense fallback={<h1>Loading dashboard...</h1>}>
|
|
561
|
+
* <Dashboard />
|
|
562
|
+
* </Suspense>
|
|
563
|
+
* </ErrorBoundary>
|
|
564
|
+
* )}
|
|
565
|
+
* </QueryErrorResetBoundary>
|
|
361
566
|
* )
|
|
362
567
|
* }
|
|
568
|
+
*
|
|
569
|
+
* function ErrorBoundary({
|
|
570
|
+
* children,
|
|
571
|
+
* onReset,
|
|
572
|
+
* fallbackRender,
|
|
573
|
+
* }: {
|
|
574
|
+
* children: ComponentChildren
|
|
575
|
+
* onReset: () => void
|
|
576
|
+
* fallbackRender: (props: {
|
|
577
|
+
* error: Error
|
|
578
|
+
* resetErrorBoundary: () => void
|
|
579
|
+
* }) => ComponentChildren
|
|
580
|
+
* }) {
|
|
581
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
582
|
+
*
|
|
583
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
584
|
+
*
|
|
585
|
+
* return children
|
|
586
|
+
* }
|
|
363
587
|
* ```
|
|
364
588
|
*/
|
|
365
589
|
export function useSuspenseQueries<
|
package/src/useSuspenseQuery.ts
CHANGED
|
@@ -22,9 +22,14 @@ import { useBaseQuery } from './useBaseQuery'
|
|
|
22
22
|
* is missing, and `status` is either `success` or `error` (with the derived flags set accordingly).
|
|
23
23
|
*
|
|
24
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:
|
|
25
28
|
* ```tsx
|
|
26
29
|
* import { Suspense } from 'preact/compat'
|
|
27
|
-
* import {
|
|
30
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
31
|
+
* import { QueryErrorResetBoundary, useSuspenseQuery } from '@tanstack/preact-query'
|
|
32
|
+
* import type { ComponentChildren } from 'preact'
|
|
28
33
|
*
|
|
29
34
|
* function Posts() {
|
|
30
35
|
* // `data` is guaranteed to be defined here — no `isPending` check needed.
|
|
@@ -35,21 +40,56 @@ import { useBaseQuery } from './useBaseQuery'
|
|
|
35
40
|
*
|
|
36
41
|
* return (
|
|
37
42
|
* <div>
|
|
38
|
-
* <h1>Posts {isFetching ?
|
|
39
|
-
*
|
|
40
|
-
*
|
|
41
|
-
*
|
|
43
|
+
* <h1>Posts {isFetching ? '(refreshing...)' : null}</h1>
|
|
44
|
+
* <ul>
|
|
45
|
+
* {data.map((post) => (
|
|
46
|
+
* <li key={post.id}>{post.title}</li>
|
|
47
|
+
* ))}
|
|
48
|
+
* </ul>
|
|
42
49
|
* </div>
|
|
43
50
|
* )
|
|
44
51
|
* }
|
|
45
52
|
*
|
|
46
53
|
* function App() {
|
|
47
54
|
* return (
|
|
48
|
-
* <
|
|
49
|
-
*
|
|
50
|
-
*
|
|
55
|
+
* <QueryErrorResetBoundary>
|
|
56
|
+
* {({ reset }) => (
|
|
57
|
+
* <ErrorBoundary
|
|
58
|
+
* onReset={reset}
|
|
59
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
60
|
+
* <div>
|
|
61
|
+
* There was an error!
|
|
62
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
63
|
+
* </div>
|
|
64
|
+
* )}
|
|
65
|
+
* >
|
|
66
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
67
|
+
* <Posts />
|
|
68
|
+
* </Suspense>
|
|
69
|
+
* </ErrorBoundary>
|
|
70
|
+
* )}
|
|
71
|
+
* </QueryErrorResetBoundary>
|
|
51
72
|
* )
|
|
52
73
|
* }
|
|
74
|
+
*
|
|
75
|
+
* function ErrorBoundary({
|
|
76
|
+
* children,
|
|
77
|
+
* onReset,
|
|
78
|
+
* fallbackRender,
|
|
79
|
+
* }: {
|
|
80
|
+
* children: ComponentChildren
|
|
81
|
+
* onReset: () => void
|
|
82
|
+
* fallbackRender: (props: {
|
|
83
|
+
* error: Error
|
|
84
|
+
* resetErrorBoundary: () => void
|
|
85
|
+
* }) => ComponentChildren
|
|
86
|
+
* }) {
|
|
87
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
88
|
+
*
|
|
89
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
90
|
+
*
|
|
91
|
+
* return children
|
|
92
|
+
* }
|
|
53
93
|
* ```
|
|
54
94
|
*/
|
|
55
95
|
export function useSuspenseQuery<
|