@tanstack/preact-query 5.102.2 → 5.102.4
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 +43 -0
- 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 +43 -0
- 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 +75 -0
- package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.cts +106 -0
- package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.d.ts +106 -0
- package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/legacy/QueryErrorResetBoundary.js +75 -0
- package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
- package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.cts +187 -0
- package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
- package/build/legacy/infiniteQueryOptions.d.ts +187 -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 +68 -0
- package/build/legacy/mutationOptions.d.cts.map +1 -1
- package/build/legacy/mutationOptions.d.ts +68 -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 +198 -0
- package/build/legacy/queryOptions.d.cts.map +1 -1
- package/build/legacy/queryOptions.d.ts +198 -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 +116 -0
- package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useInfiniteQuery.d.ts +116 -0
- package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useInfiniteQuery.js.map +1 -1
- package/build/legacy/useIsFetching.cjs +20 -0
- package/build/legacy/useIsFetching.cjs.map +1 -1
- package/build/legacy/useIsFetching.d.cts +20 -0
- package/build/legacy/useIsFetching.d.cts.map +1 -1
- package/build/legacy/useIsFetching.d.ts +20 -0
- package/build/legacy/useIsFetching.d.ts.map +1 -1
- package/build/legacy/useIsFetching.js +20 -0
- package/build/legacy/useIsFetching.js.map +1 -1
- package/build/legacy/useMutation.cjs +67 -0
- package/build/legacy/useMutation.cjs.map +1 -1
- package/build/legacy/useMutation.d.cts +67 -0
- package/build/legacy/useMutation.d.cts.map +1 -1
- package/build/legacy/useMutation.d.ts +67 -0
- package/build/legacy/useMutation.d.ts.map +1 -1
- package/build/legacy/useMutation.js +67 -0
- package/build/legacy/useMutation.js.map +1 -1
- package/build/legacy/useMutationState.cjs +74 -0
- package/build/legacy/useMutationState.cjs.map +1 -1
- package/build/legacy/useMutationState.d.cts +74 -0
- package/build/legacy/useMutationState.d.cts.map +1 -1
- package/build/legacy/useMutationState.d.ts +74 -0
- package/build/legacy/useMutationState.d.ts.map +1 -1
- package/build/legacy/useMutationState.js +74 -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 +36 -1
- 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 +36 -1
- package/build/legacy/usePrefetchQuery.js.map +1 -1
- package/build/legacy/useQueries.cjs +50 -0
- package/build/legacy/useQueries.cjs.map +1 -1
- package/build/legacy/useQueries.d.cts +85 -2
- package/build/legacy/useQueries.d.cts.map +1 -1
- package/build/legacy/useQueries.d.ts +85 -2
- package/build/legacy/useQueries.d.ts.map +1 -1
- package/build/legacy/useQueries.js +50 -0
- package/build/legacy/useQueries.js.map +1 -1
- package/build/legacy/useQuery.cjs.map +1 -1
- package/build/legacy/useQuery.d.cts +134 -0
- package/build/legacy/useQuery.d.cts.map +1 -1
- package/build/legacy/useQuery.d.ts +134 -0
- package/build/legacy/useQuery.d.ts.map +1 -1
- package/build/legacy/useQuery.js.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.cjs +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.cts +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.d.ts +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseInfiniteQuery.js +48 -0
- package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/legacy/useSuspenseQueries.cjs.map +1 -1
- package/build/legacy/useSuspenseQueries.d.cts +128 -2
- package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
- package/build/legacy/useSuspenseQueries.d.ts +128 -2
- package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
- package/build/legacy/useSuspenseQueries.js.map +1 -1
- package/build/legacy/useSuspenseQuery.cjs +43 -0
- package/build/legacy/useSuspenseQuery.cjs.map +1 -1
- package/build/legacy/useSuspenseQuery.d.cts +43 -0
- package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
- package/build/legacy/useSuspenseQuery.d.ts +43 -0
- package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
- package/build/legacy/useSuspenseQuery.js +43 -0
- package/build/legacy/useSuspenseQuery.js.map +1 -1
- package/build/legacy/utils.cjs.map +1 -1
- package/build/legacy/utils.d.cts.map +1 -1
- package/build/legacy/utils.d.ts.map +1 -1
- package/build/legacy/utils.js.map +1 -1
- package/build/modern/HydrationBoundary.cjs +43 -0
- 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 +43 -0
- 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 +75 -0
- package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.cts +106 -0
- package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.d.ts +106 -0
- package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
- package/build/modern/QueryErrorResetBoundary.js +75 -0
- package/build/modern/QueryErrorResetBoundary.js.map +1 -1
- package/build/modern/infiniteQueryOptions.cjs.map +1 -1
- package/build/modern/infiniteQueryOptions.d.cts +187 -0
- package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
- package/build/modern/infiniteQueryOptions.d.ts +187 -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 +68 -0
- package/build/modern/mutationOptions.d.cts.map +1 -1
- package/build/modern/mutationOptions.d.ts +68 -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 +198 -0
- package/build/modern/queryOptions.d.cts.map +1 -1
- package/build/modern/queryOptions.d.ts +198 -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 +116 -0
- package/build/modern/useInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useInfiniteQuery.d.ts +116 -0
- package/build/modern/useInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useInfiniteQuery.js.map +1 -1
- package/build/modern/useIsFetching.cjs +20 -0
- package/build/modern/useIsFetching.cjs.map +1 -1
- package/build/modern/useIsFetching.d.cts +20 -0
- package/build/modern/useIsFetching.d.cts.map +1 -1
- package/build/modern/useIsFetching.d.ts +20 -0
- package/build/modern/useIsFetching.d.ts.map +1 -1
- package/build/modern/useIsFetching.js +20 -0
- package/build/modern/useIsFetching.js.map +1 -1
- package/build/modern/useMutation.cjs +67 -0
- package/build/modern/useMutation.cjs.map +1 -1
- package/build/modern/useMutation.d.cts +67 -0
- package/build/modern/useMutation.d.cts.map +1 -1
- package/build/modern/useMutation.d.ts +67 -0
- package/build/modern/useMutation.d.ts.map +1 -1
- package/build/modern/useMutation.js +67 -0
- package/build/modern/useMutation.js.map +1 -1
- package/build/modern/useMutationState.cjs +74 -0
- package/build/modern/useMutationState.cjs.map +1 -1
- package/build/modern/useMutationState.d.cts +74 -0
- package/build/modern/useMutationState.d.cts.map +1 -1
- package/build/modern/useMutationState.d.ts +74 -0
- package/build/modern/useMutationState.d.ts.map +1 -1
- package/build/modern/useMutationState.js +74 -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 +36 -1
- 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 +36 -1
- package/build/modern/usePrefetchQuery.js.map +1 -1
- package/build/modern/useQueries.cjs +50 -0
- package/build/modern/useQueries.cjs.map +1 -1
- package/build/modern/useQueries.d.cts +85 -2
- package/build/modern/useQueries.d.cts.map +1 -1
- package/build/modern/useQueries.d.ts +85 -2
- package/build/modern/useQueries.d.ts.map +1 -1
- package/build/modern/useQueries.js +50 -0
- package/build/modern/useQueries.js.map +1 -1
- package/build/modern/useQuery.cjs.map +1 -1
- package/build/modern/useQuery.d.cts +134 -0
- package/build/modern/useQuery.d.cts.map +1 -1
- package/build/modern/useQuery.d.ts +134 -0
- package/build/modern/useQuery.d.ts.map +1 -1
- package/build/modern/useQuery.js.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.cjs +48 -0
- package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.cts +48 -0
- package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.d.ts +48 -0
- package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseInfiniteQuery.js +48 -0
- package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
- package/build/modern/useSuspenseQueries.cjs.map +1 -1
- package/build/modern/useSuspenseQueries.d.cts +128 -2
- package/build/modern/useSuspenseQueries.d.cts.map +1 -1
- package/build/modern/useSuspenseQueries.d.ts +128 -2
- package/build/modern/useSuspenseQueries.d.ts.map +1 -1
- package/build/modern/useSuspenseQueries.js.map +1 -1
- package/build/modern/useSuspenseQuery.cjs +43 -0
- package/build/modern/useSuspenseQuery.cjs.map +1 -1
- package/build/modern/useSuspenseQuery.d.cts +43 -0
- package/build/modern/useSuspenseQuery.d.cts.map +1 -1
- package/build/modern/useSuspenseQuery.d.ts +43 -0
- package/build/modern/useSuspenseQuery.d.ts.map +1 -1
- package/build/modern/useSuspenseQuery.js +43 -0
- package/build/modern/useSuspenseQuery.js.map +1 -1
- package/build/modern/utils.cjs.map +1 -1
- package/build/modern/utils.d.cts.map +1 -1
- package/build/modern/utils.d.ts.map +1 -1
- package/build/modern/utils.js.map +1 -1
- package/package.json +3 -3
- package/src/HydrationBoundary.tsx +61 -1
- package/src/IsRestoringProvider.ts +12 -0
- package/src/QueryClientProvider.tsx +41 -0
- package/src/QueryErrorResetBoundary.tsx +109 -0
- package/src/infiniteQueryOptions.ts +187 -0
- package/src/mutationOptions.ts +68 -0
- package/src/queryOptions.ts +198 -0
- package/src/types.ts +228 -2
- package/src/useInfiniteQuery.ts +116 -0
- package/src/useIsFetching.ts +20 -0
- package/src/useMutation.ts +67 -0
- package/src/useMutationState.ts +74 -0
- package/src/usePrefetchInfiniteQuery.tsx +44 -0
- package/src/usePrefetchQuery.tsx +36 -1
- package/src/useQueries.ts +85 -2
- package/src/useQuery.ts +134 -0
- package/src/useSuspenseInfiniteQuery.ts +48 -0
- package/src/useSuspenseQueries.ts +128 -2
- package/src/useSuspenseQuery.ts +43 -0
- package/src/utils.ts +0 -1
package/src/useQuery.ts
CHANGED
|
@@ -12,6 +12,33 @@ import type {
|
|
|
12
12
|
} from './types'
|
|
13
13
|
import { useBaseQuery } from './useBaseQuery'
|
|
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/preact-query'
|
|
29
|
+
*
|
|
30
|
+
* function Posts() {
|
|
31
|
+
* // `data` is `Post[]`, never `undefined`, thanks to `initialData`.
|
|
32
|
+
* const { data } = useQuery({
|
|
33
|
+
* queryKey: ['posts'],
|
|
34
|
+
* queryFn: fetchPosts,
|
|
35
|
+
* initialData: [],
|
|
36
|
+
* })
|
|
37
|
+
*
|
|
38
|
+
* return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*/
|
|
15
42
|
export function useQuery<
|
|
16
43
|
TQueryFnData = unknown,
|
|
17
44
|
TError = DefaultError,
|
|
@@ -22,6 +49,41 @@ export function useQuery<
|
|
|
22
49
|
queryClient?: QueryClient,
|
|
23
50
|
): DefinedUseQueryResult<TData, TError>
|
|
24
51
|
|
|
52
|
+
/**
|
|
53
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
54
|
+
* @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
|
|
55
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
56
|
+
* be used.
|
|
57
|
+
* @returns The current query result. `status` is `pending` if there is no cached data and no query attempt
|
|
58
|
+
* has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to
|
|
59
|
+
* display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.
|
|
60
|
+
*
|
|
61
|
+
* @example
|
|
62
|
+
* ```tsx
|
|
63
|
+
* import { queryOptions, useQuery } from '@tanstack/preact-query'
|
|
64
|
+
*
|
|
65
|
+
* const postsOptions = queryOptions({
|
|
66
|
+
* queryKey: ['posts'],
|
|
67
|
+
* queryFn: fetchPosts,
|
|
68
|
+
* })
|
|
69
|
+
*
|
|
70
|
+
* function Posts() {
|
|
71
|
+
* const { status, data, error, isFetching } = useQuery(postsOptions)
|
|
72
|
+
*
|
|
73
|
+
* if (status === 'pending') return 'Loading...'
|
|
74
|
+
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
75
|
+
*
|
|
76
|
+
* return (
|
|
77
|
+
* <div>
|
|
78
|
+
* {data.map((post) => (
|
|
79
|
+
* <p key={post.id}>{post.title}</p>
|
|
80
|
+
* ))}
|
|
81
|
+
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
82
|
+
* </div>
|
|
83
|
+
* )
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
25
87
|
export function useQuery<
|
|
26
88
|
TQueryFnData = unknown,
|
|
27
89
|
TError = DefaultError,
|
|
@@ -32,6 +94,78 @@ export function useQuery<
|
|
|
32
94
|
queryClient?: QueryClient,
|
|
33
95
|
): UseQueryResult<TData, TError>
|
|
34
96
|
|
|
97
|
+
/**
|
|
98
|
+
* @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.
|
|
99
|
+
* @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.
|
|
100
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
101
|
+
* be used.
|
|
102
|
+
* @returns The current query result. `status` is `pending` if there is no cached data and no query attempt
|
|
103
|
+
* has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to
|
|
104
|
+
* display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.
|
|
105
|
+
*
|
|
106
|
+
* @example
|
|
107
|
+
* ```tsx
|
|
108
|
+
* import { queryOptions, useQuery } from '@tanstack/preact-query'
|
|
109
|
+
*
|
|
110
|
+
* const postsOptions = queryOptions({
|
|
111
|
+
* queryKey: ['posts'],
|
|
112
|
+
* queryFn: fetchPosts,
|
|
113
|
+
* })
|
|
114
|
+
*
|
|
115
|
+
* function Posts() {
|
|
116
|
+
* const { status, data, error, isFetching } = useQuery(postsOptions)
|
|
117
|
+
*
|
|
118
|
+
* if (status === 'pending') return 'Loading...'
|
|
119
|
+
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
120
|
+
*
|
|
121
|
+
* return (
|
|
122
|
+
* <div>
|
|
123
|
+
* {data.map((post) => (
|
|
124
|
+
* <p key={post.id}>{post.title}</p>
|
|
125
|
+
* ))}
|
|
126
|
+
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
127
|
+
* </div>
|
|
128
|
+
* )
|
|
129
|
+
* }
|
|
130
|
+
* ```
|
|
131
|
+
*
|
|
132
|
+
* @example
|
|
133
|
+
* A dependent query, only enabled once `postId` is set:
|
|
134
|
+
* ```tsx
|
|
135
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
136
|
+
*
|
|
137
|
+
* function Post({ postId }: { postId: number | undefined }) {
|
|
138
|
+
* const { data } = useQuery({
|
|
139
|
+
* queryKey: ['post', postId],
|
|
140
|
+
* queryFn: () => fetchPost(postId!),
|
|
141
|
+
* enabled: postId != null,
|
|
142
|
+
* })
|
|
143
|
+
*
|
|
144
|
+
* return <h1>{data?.title}</h1>
|
|
145
|
+
* }
|
|
146
|
+
* ```
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* Seeding a detail query from an already-cached list, to skip the loading state:
|
|
150
|
+
* ```tsx
|
|
151
|
+
* import { useQuery, useQueryClient } from '@tanstack/preact-query'
|
|
152
|
+
*
|
|
153
|
+
* function Post({ postId }: { postId: number }) {
|
|
154
|
+
* const queryClient = useQueryClient()
|
|
155
|
+
*
|
|
156
|
+
* const { data } = useQuery({
|
|
157
|
+
* queryKey: ['post', postId],
|
|
158
|
+
* queryFn: () => fetchPost(postId),
|
|
159
|
+
* initialData: () =>
|
|
160
|
+
* queryClient
|
|
161
|
+
* .getQueryData<Array<Post>>(['posts'])
|
|
162
|
+
* ?.find((post) => post.id === postId),
|
|
163
|
+
* })
|
|
164
|
+
*
|
|
165
|
+
* return <h1>{data?.title}</h1>
|
|
166
|
+
* }
|
|
167
|
+
* ```
|
|
168
|
+
*/
|
|
35
169
|
export function useQuery<
|
|
36
170
|
TQueryFnData = unknown,
|
|
37
171
|
TError = DefaultError,
|
|
@@ -15,6 +15,54 @@ import type {
|
|
|
15
15
|
} from './types'
|
|
16
16
|
import { useBaseQuery } from './useBaseQuery'
|
|
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
|
+
* @param options - The {@link UseSuspenseInfiniteQueryOptions} to use — the same options as `useInfiniteQuery`, minus the ones listed above.
|
|
25
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
26
|
+
* be used.
|
|
27
|
+
* @returns The same object as `useInfiniteQuery`, except that `data` is guaranteed to be defined,
|
|
28
|
+
* `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived flags set
|
|
29
|
+
* accordingly).
|
|
30
|
+
*
|
|
31
|
+
* @example
|
|
32
|
+
* ```tsx
|
|
33
|
+
* import { Suspense } from 'preact/compat'
|
|
34
|
+
* import { useSuspenseInfiniteQuery } from '@tanstack/preact-query'
|
|
35
|
+
*
|
|
36
|
+
* function Projects() {
|
|
37
|
+
* // `data` is guaranteed to be defined here — no `isPending` check needed.
|
|
38
|
+
* const { data, fetchNextPage, hasNextPage } = useSuspenseInfiniteQuery({
|
|
39
|
+
* queryKey: ['projects'],
|
|
40
|
+
* queryFn: ({ pageParam }) => fetchProjects(pageParam),
|
|
41
|
+
* initialPageParam: 0,
|
|
42
|
+
* getNextPageParam: (lastPage) => lastPage.nextId,
|
|
43
|
+
* })
|
|
44
|
+
*
|
|
45
|
+
* return (
|
|
46
|
+
* <div>
|
|
47
|
+
* {data.pages.map((page) =>
|
|
48
|
+
* page.projects.map((project) => <p key={project.id}>{project.name}</p>),
|
|
49
|
+
* )}
|
|
50
|
+
* <button onClick={() => fetchNextPage()} disabled={!hasNextPage}>
|
|
51
|
+
* Load More
|
|
52
|
+
* </button>
|
|
53
|
+
* </div>
|
|
54
|
+
* )
|
|
55
|
+
* }
|
|
56
|
+
*
|
|
57
|
+
* function App() {
|
|
58
|
+
* return (
|
|
59
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
60
|
+
* <Projects />
|
|
61
|
+
* </Suspense>
|
|
62
|
+
* )
|
|
63
|
+
* }
|
|
64
|
+
* ```
|
|
65
|
+
*/
|
|
18
66
|
export function useSuspenseInfiniteQuery<
|
|
19
67
|
TQueryFnData,
|
|
20
68
|
TError = DefaultError,
|
|
@@ -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,133 @@ 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 each `query` can't have
|
|
185
|
+
* `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
|
+
* ```tsx
|
|
200
|
+
* import { Suspense } from 'preact/compat'
|
|
201
|
+
* import { useSuspenseQueries } from '@tanstack/preact-query'
|
|
202
|
+
*
|
|
203
|
+
* function Posts({ ids }: { ids: Array<number> }) {
|
|
204
|
+
* // Every result is guaranteed to be defined — no per-query `isPending` check needed.
|
|
205
|
+
* const results = useSuspenseQueries({
|
|
206
|
+
* queries: ids.map((id) => ({
|
|
207
|
+
* queryKey: ['post', id],
|
|
208
|
+
* queryFn: () => fetchPost(id),
|
|
209
|
+
* })),
|
|
210
|
+
* })
|
|
211
|
+
*
|
|
212
|
+
* return (
|
|
213
|
+
* <ul>
|
|
214
|
+
* {results.map((result) => (
|
|
215
|
+
* <li key={result.data.id}>{result.data.title}</li>
|
|
216
|
+
* ))}
|
|
217
|
+
* </ul>
|
|
218
|
+
* )
|
|
219
|
+
* }
|
|
220
|
+
*
|
|
221
|
+
* function App() {
|
|
222
|
+
* return (
|
|
223
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
224
|
+
* <Posts ids={[1, 2, 3]} />
|
|
225
|
+
* </Suspense>
|
|
226
|
+
* )
|
|
227
|
+
* }
|
|
228
|
+
* ```
|
|
229
|
+
*/
|
|
165
230
|
export function useSuspenseQueries<
|
|
166
231
|
T extends Array<any>,
|
|
167
232
|
TCombinedResult = SuspenseQueriesResults<T>,
|
|
168
233
|
>(
|
|
169
234
|
options: {
|
|
235
|
+
/**
|
|
236
|
+
* An array with query option objects identical to `useSuspenseQuery`.
|
|
237
|
+
*/
|
|
170
238
|
queries:
|
|
171
239
|
| readonly [...SuspenseQueriesOptions<T>]
|
|
172
240
|
| readonly [...{ [K in keyof T]: GetUseSuspenseQueryOptions<T[K]> }]
|
|
241
|
+
/**
|
|
242
|
+
* Use this to combine the results of the queries into a single value. The result will be structurally
|
|
243
|
+
* shared to be as referentially stable as possible.
|
|
244
|
+
*/
|
|
173
245
|
combine?: (result: SuspenseQueriesResults<T>) => TCombinedResult
|
|
174
246
|
},
|
|
175
247
|
queryClient?: QueryClient,
|
|
176
248
|
): TCombinedResult
|
|
177
249
|
|
|
250
|
+
/**
|
|
251
|
+
* The options for `useSuspenseQueries` are the same as for `useQueries`, except that each `query` can't have
|
|
252
|
+
* `throwOnError`, `enabled`, or `placeholderData`.
|
|
253
|
+
*
|
|
254
|
+
* @param options - The `queries` array to run in Suspense, and an optional `combine` function.
|
|
255
|
+
* @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
|
|
256
|
+
* will be used.
|
|
257
|
+
* @returns The same structure as `useQueries`, except that for each `query`, `data` is guaranteed to be
|
|
258
|
+
* defined, `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived
|
|
259
|
+
* flags set accordingly).
|
|
260
|
+
*
|
|
261
|
+
* Caveat: the component will only re-mount after all queries have finished loading. Hence, if a query has gone
|
|
262
|
+
* stale in the time it took for all the queries to complete, it will be fetched again at re-mount. To avoid
|
|
263
|
+
* this, make sure to set a high enough `staleTime`. Cancellation does not work.
|
|
264
|
+
*
|
|
265
|
+
* @example
|
|
266
|
+
* ```tsx
|
|
267
|
+
* import { Suspense } from 'preact/compat'
|
|
268
|
+
* import { useSuspenseQueries } from '@tanstack/preact-query'
|
|
269
|
+
*
|
|
270
|
+
* function Posts({ ids }: { ids: Array<number> }) {
|
|
271
|
+
* // Every result is guaranteed to be defined — no per-query `isPending` check needed.
|
|
272
|
+
* const results = useSuspenseQueries({
|
|
273
|
+
* queries: ids.map((id) => ({
|
|
274
|
+
* queryKey: ['post', id],
|
|
275
|
+
* queryFn: () => fetchPost(id),
|
|
276
|
+
* })),
|
|
277
|
+
* })
|
|
278
|
+
*
|
|
279
|
+
* return (
|
|
280
|
+
* <ul>
|
|
281
|
+
* {results.map((result) => (
|
|
282
|
+
* <li key={result.data.id}>{result.data.title}</li>
|
|
283
|
+
* ))}
|
|
284
|
+
* </ul>
|
|
285
|
+
* )
|
|
286
|
+
* }
|
|
287
|
+
*
|
|
288
|
+
* function App() {
|
|
289
|
+
* return (
|
|
290
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
291
|
+
* <Posts ids={[1, 2, 3]} />
|
|
292
|
+
* </Suspense>
|
|
293
|
+
* )
|
|
294
|
+
* }
|
|
295
|
+
* ```
|
|
296
|
+
*/
|
|
178
297
|
export function useSuspenseQueries<
|
|
179
298
|
T extends Array<any>,
|
|
180
299
|
TCombinedResult = SuspenseQueriesResults<T>,
|
|
181
300
|
>(
|
|
182
301
|
options: {
|
|
302
|
+
/**
|
|
303
|
+
* An array with query option objects identical to `useSuspenseQuery`.
|
|
304
|
+
*/
|
|
183
305
|
queries: readonly [...SuspenseQueriesOptions<T>]
|
|
306
|
+
/**
|
|
307
|
+
* Use this to combine the results of the queries into a single value. The result will be structurally
|
|
308
|
+
* shared to be as referentially stable as possible.
|
|
309
|
+
*/
|
|
184
310
|
combine?: (result: SuspenseQueriesResults<T>) => TCombinedResult
|
|
185
311
|
},
|
|
186
312
|
queryClient?: QueryClient,
|
package/src/useSuspenseQuery.ts
CHANGED
|
@@ -5,6 +5,49 @@ import { defaultThrowOnError } from './suspense'
|
|
|
5
5
|
import type { UseSuspenseQueryOptions, UseSuspenseQueryResult } from './types'
|
|
6
6
|
import { useBaseQuery } from './useBaseQuery'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* The options for `useSuspenseQuery` are the same as for `useQuery`, except for `throwOnError`, `enabled`, and
|
|
10
|
+
* `placeholderData`.
|
|
11
|
+
*
|
|
12
|
+
* Caveat: cancellation does not work.
|
|
13
|
+
*
|
|
14
|
+
* @param options - The {@link UseSuspenseQueryOptions} to use — the same options as `useQuery`, minus the ones listed above.
|
|
15
|
+
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
16
|
+
* be used.
|
|
17
|
+
* @returns The same object as `useQuery`, except that `data` is guaranteed to be defined, `isPlaceholderData`
|
|
18
|
+
* is missing, and `status` is either `success` or `error` (with the derived flags set accordingly).
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```tsx
|
|
22
|
+
* import { Suspense } from 'preact/compat'
|
|
23
|
+
* import { useSuspenseQuery } from '@tanstack/preact-query'
|
|
24
|
+
*
|
|
25
|
+
* function Posts() {
|
|
26
|
+
* // `data` is guaranteed to be defined here — no `isPending` check needed.
|
|
27
|
+
* const { data, isFetching } = useSuspenseQuery({
|
|
28
|
+
* queryKey: ['posts'],
|
|
29
|
+
* queryFn: fetchPosts,
|
|
30
|
+
* })
|
|
31
|
+
*
|
|
32
|
+
* return (
|
|
33
|
+
* <div>
|
|
34
|
+
* <h1>Posts {isFetching ? <Spinner /> : null}</h1>
|
|
35
|
+
* {data.map((post) => (
|
|
36
|
+
* <p key={post.id}>{post.title}</p>
|
|
37
|
+
* ))}
|
|
38
|
+
* </div>
|
|
39
|
+
* )
|
|
40
|
+
* }
|
|
41
|
+
*
|
|
42
|
+
* function App() {
|
|
43
|
+
* return (
|
|
44
|
+
* <Suspense fallback={<h1>Loading posts...</h1>}>
|
|
45
|
+
* <Posts />
|
|
46
|
+
* </Suspense>
|
|
47
|
+
* )
|
|
48
|
+
* }
|
|
49
|
+
* ```
|
|
50
|
+
*/
|
|
8
51
|
export function useSuspenseQuery<
|
|
9
52
|
TQueryFnData = unknown,
|
|
10
53
|
TError = DefaultError,
|