@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
|
@@ -18,14 +18,22 @@ import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
|
18
18
|
* import { useQuery } from '@tanstack/preact-query'
|
|
19
19
|
*
|
|
20
20
|
* function Posts() {
|
|
21
|
-
* // `data` is `Post[]`, never `undefined`, thanks to `initialData
|
|
22
|
-
*
|
|
21
|
+
* // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
|
|
22
|
+
* // so the list stays visible alongside the error.
|
|
23
|
+
* const { data, isError, error } = useQuery({
|
|
23
24
|
* queryKey: ['posts'],
|
|
24
25
|
* queryFn: fetchPosts,
|
|
25
26
|
* initialData: [],
|
|
26
27
|
* })
|
|
27
28
|
*
|
|
28
|
-
* return
|
|
29
|
+
* return (
|
|
30
|
+
* <div>
|
|
31
|
+
* {isError ? <span>Error: {error.message}</span> : null}
|
|
32
|
+
* <ul>
|
|
33
|
+
* {data.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
34
|
+
* </ul>
|
|
35
|
+
* </div>
|
|
36
|
+
* )
|
|
29
37
|
* }
|
|
30
38
|
* ```
|
|
31
39
|
*/
|
|
@@ -35,30 +43,30 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
35
43
|
* @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
|
|
36
44
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
37
45
|
* be used.
|
|
38
|
-
* @returns The current query result. `status` is `pending` if there is no cached data
|
|
39
|
-
*
|
|
40
|
-
*
|
|
46
|
+
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
47
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
|
|
48
|
+
* are derived booleans for convenience.
|
|
41
49
|
*
|
|
42
50
|
* @example
|
|
43
51
|
* ```tsx
|
|
44
|
-
* import {
|
|
45
|
-
*
|
|
46
|
-
* const postsOptions = queryOptions({
|
|
47
|
-
* queryKey: ['posts'],
|
|
48
|
-
* queryFn: fetchPosts,
|
|
49
|
-
* })
|
|
52
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
50
53
|
*
|
|
51
54
|
* function Posts() {
|
|
52
|
-
* const { status, data, error, isFetching } = useQuery(
|
|
55
|
+
* const { status, data, error, isFetching } = useQuery({
|
|
56
|
+
* queryKey: ['posts'],
|
|
57
|
+
* queryFn: fetchPosts,
|
|
58
|
+
* })
|
|
53
59
|
*
|
|
54
60
|
* if (status === 'pending') return 'Loading...'
|
|
55
61
|
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
56
62
|
*
|
|
57
63
|
* return (
|
|
58
64
|
* <div>
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
65
|
+
* <ul>
|
|
66
|
+
* {data.map((post) => (
|
|
67
|
+
* <li key={post.id}>{post.title}</li>
|
|
68
|
+
* ))}
|
|
69
|
+
* </ul>
|
|
62
70
|
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
63
71
|
* </div>
|
|
64
72
|
* )
|
|
@@ -79,7 +87,11 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
79
87
|
* if (isPending) return 'Loading...'
|
|
80
88
|
* if (isError) return <span>Error: {error.message}</span>
|
|
81
89
|
*
|
|
82
|
-
* return
|
|
90
|
+
* return (
|
|
91
|
+
* <ul>
|
|
92
|
+
* {data.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
93
|
+
* </ul>
|
|
94
|
+
* )
|
|
83
95
|
* }
|
|
84
96
|
* ```
|
|
85
97
|
*/
|
|
@@ -89,30 +101,30 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
89
101
|
* @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.
|
|
90
102
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
91
103
|
* be used.
|
|
92
|
-
* @returns The current query result. `status` is `pending` if there is no cached data
|
|
93
|
-
*
|
|
94
|
-
*
|
|
104
|
+
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
105
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
|
|
106
|
+
* are derived booleans for convenience.
|
|
95
107
|
*
|
|
96
108
|
* @example
|
|
97
109
|
* ```tsx
|
|
98
|
-
* import {
|
|
99
|
-
*
|
|
100
|
-
* const postsOptions = queryOptions({
|
|
101
|
-
* queryKey: ['posts'],
|
|
102
|
-
* queryFn: fetchPosts,
|
|
103
|
-
* })
|
|
110
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
104
111
|
*
|
|
105
112
|
* function Posts() {
|
|
106
|
-
* const { status, data, error, isFetching } = useQuery(
|
|
113
|
+
* const { status, data, error, isFetching } = useQuery({
|
|
114
|
+
* queryKey: ['posts'],
|
|
115
|
+
* queryFn: fetchPosts,
|
|
116
|
+
* })
|
|
107
117
|
*
|
|
108
118
|
* if (status === 'pending') return 'Loading...'
|
|
109
119
|
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
110
120
|
*
|
|
111
121
|
* return (
|
|
112
122
|
* <div>
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
123
|
+
* <ul>
|
|
124
|
+
* {data.map((post) => (
|
|
125
|
+
* <li key={post.id}>{post.title}</li>
|
|
126
|
+
* ))}
|
|
127
|
+
* </ul>
|
|
116
128
|
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
117
129
|
* </div>
|
|
118
130
|
* )
|
|
@@ -120,6 +132,26 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
120
132
|
* ```
|
|
121
133
|
*
|
|
122
134
|
* @example
|
|
135
|
+
* `select` derives whatever `data` a component needs from the cached value, without changing what's
|
|
136
|
+
* actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:
|
|
137
|
+
* ```tsx
|
|
138
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
139
|
+
*
|
|
140
|
+
* function PostCount() {
|
|
141
|
+
* const { data, isPending, isError, error } = useQuery({
|
|
142
|
+
* queryKey: ['posts'],
|
|
143
|
+
* queryFn: fetchPosts,
|
|
144
|
+
* select: (posts) => posts.length,
|
|
145
|
+
* })
|
|
146
|
+
*
|
|
147
|
+
* if (isPending) return 'Loading...'
|
|
148
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
149
|
+
*
|
|
150
|
+
* return <span>{data} posts</span>
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
123
155
|
* A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the
|
|
124
156
|
* loading state doesn't show while the query is disabled:
|
|
125
157
|
* ```tsx
|
|
@@ -141,6 +173,28 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
141
173
|
* ```
|
|
142
174
|
*
|
|
143
175
|
* @example
|
|
176
|
+
* The same dependent query, type safe: `skipToken` disables the query without needing the
|
|
177
|
+
* non-null assertion above, since `queryFn` is only ever called when `postId` is defined.
|
|
178
|
+
* `refetch` doesn't work while `queryFn` is `skipToken` — use `enabled: false` instead if you
|
|
179
|
+
* need to trigger the query manually:
|
|
180
|
+
* ```tsx
|
|
181
|
+
* import { skipToken, useQuery } from '@tanstack/preact-query'
|
|
182
|
+
*
|
|
183
|
+
* function Post({ postId }: { postId: number | undefined }) {
|
|
184
|
+
* const { data, isLoading, isError, error } = useQuery({
|
|
185
|
+
* queryKey: ['post', postId],
|
|
186
|
+
* queryFn: postId != null ? () => fetchPost(postId) : skipToken,
|
|
187
|
+
* })
|
|
188
|
+
*
|
|
189
|
+
* if (postId == null) return 'Select a post'
|
|
190
|
+
* if (isLoading) return 'Loading...'
|
|
191
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
192
|
+
*
|
|
193
|
+
* return <h1>{data?.title}</h1>
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
144
198
|
* Seeding a detail query from an already-cached list, to skip the loading state:
|
|
145
199
|
* ```tsx
|
|
146
200
|
* import { useQuery, useQueryClient } from '@tanstack/preact-query'
|
|
@@ -148,7 +202,7 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
148
202
|
* function Post({ postId }: { postId: number }) {
|
|
149
203
|
* const queryClient = useQueryClient()
|
|
150
204
|
*
|
|
151
|
-
* const { data } = useQuery({
|
|
205
|
+
* const { data, isError, error } = useQuery({
|
|
152
206
|
* queryKey: ['post', postId],
|
|
153
207
|
* queryFn: () => fetchPost(postId),
|
|
154
208
|
* initialData: () =>
|
|
@@ -157,6 +211,8 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
157
211
|
* ?.find((post) => post.id === postId),
|
|
158
212
|
* })
|
|
159
213
|
*
|
|
214
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
215
|
+
*
|
|
160
216
|
* return <h1>{data?.title}</h1>
|
|
161
217
|
* }
|
|
162
218
|
* ```
|
|
@@ -170,15 +226,19 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
170
226
|
* function Posts() {
|
|
171
227
|
* const [page, setPage] = useState(0)
|
|
172
228
|
*
|
|
173
|
-
* const { data, isPlaceholderData } = useQuery({
|
|
229
|
+
* const { data, isPlaceholderData, isError, error } = useQuery({
|
|
174
230
|
* queryKey: ['posts', page],
|
|
175
231
|
* queryFn: () => fetchPosts(page),
|
|
176
232
|
* placeholderData: keepPreviousData,
|
|
177
233
|
* })
|
|
178
234
|
*
|
|
235
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
236
|
+
*
|
|
179
237
|
* return (
|
|
180
238
|
* <div>
|
|
181
|
-
*
|
|
239
|
+
* <ul>
|
|
240
|
+
* {data?.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
241
|
+
* </ul>
|
|
182
242
|
* <button
|
|
183
243
|
* disabled={isPlaceholderData}
|
|
184
244
|
* onClick={() => setPage((old) => old + 1)}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.cts","names":[],"sources":["../../src/useQuery.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQuery.d.cts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiDgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2DhB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4JT,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}
|
|
@@ -18,14 +18,22 @@ import { DefaultError, QueryClient, QueryKey } from "@tanstack/query-core";
|
|
|
18
18
|
* import { useQuery } from '@tanstack/preact-query'
|
|
19
19
|
*
|
|
20
20
|
* function Posts() {
|
|
21
|
-
* // `data` is `Post[]`, never `undefined`, thanks to `initialData
|
|
22
|
-
*
|
|
21
|
+
* // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
|
|
22
|
+
* // so the list stays visible alongside the error.
|
|
23
|
+
* const { data, isError, error } = useQuery({
|
|
23
24
|
* queryKey: ['posts'],
|
|
24
25
|
* queryFn: fetchPosts,
|
|
25
26
|
* initialData: [],
|
|
26
27
|
* })
|
|
27
28
|
*
|
|
28
|
-
* return
|
|
29
|
+
* return (
|
|
30
|
+
* <div>
|
|
31
|
+
* {isError ? <span>Error: {error.message}</span> : null}
|
|
32
|
+
* <ul>
|
|
33
|
+
* {data.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
34
|
+
* </ul>
|
|
35
|
+
* </div>
|
|
36
|
+
* )
|
|
29
37
|
* }
|
|
30
38
|
* ```
|
|
31
39
|
*/
|
|
@@ -35,30 +43,30 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
35
43
|
* @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
|
|
36
44
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
37
45
|
* be used.
|
|
38
|
-
* @returns The current query result. `status` is `pending` if there is no cached data
|
|
39
|
-
*
|
|
40
|
-
*
|
|
46
|
+
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
47
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
|
|
48
|
+
* are derived booleans for convenience.
|
|
41
49
|
*
|
|
42
50
|
* @example
|
|
43
51
|
* ```tsx
|
|
44
|
-
* import {
|
|
45
|
-
*
|
|
46
|
-
* const postsOptions = queryOptions({
|
|
47
|
-
* queryKey: ['posts'],
|
|
48
|
-
* queryFn: fetchPosts,
|
|
49
|
-
* })
|
|
52
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
50
53
|
*
|
|
51
54
|
* function Posts() {
|
|
52
|
-
* const { status, data, error, isFetching } = useQuery(
|
|
55
|
+
* const { status, data, error, isFetching } = useQuery({
|
|
56
|
+
* queryKey: ['posts'],
|
|
57
|
+
* queryFn: fetchPosts,
|
|
58
|
+
* })
|
|
53
59
|
*
|
|
54
60
|
* if (status === 'pending') return 'Loading...'
|
|
55
61
|
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
56
62
|
*
|
|
57
63
|
* return (
|
|
58
64
|
* <div>
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
65
|
+
* <ul>
|
|
66
|
+
* {data.map((post) => (
|
|
67
|
+
* <li key={post.id}>{post.title}</li>
|
|
68
|
+
* ))}
|
|
69
|
+
* </ul>
|
|
62
70
|
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
63
71
|
* </div>
|
|
64
72
|
* )
|
|
@@ -79,7 +87,11 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
79
87
|
* if (isPending) return 'Loading...'
|
|
80
88
|
* if (isError) return <span>Error: {error.message}</span>
|
|
81
89
|
*
|
|
82
|
-
* return
|
|
90
|
+
* return (
|
|
91
|
+
* <ul>
|
|
92
|
+
* {data.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
93
|
+
* </ul>
|
|
94
|
+
* )
|
|
83
95
|
* }
|
|
84
96
|
* ```
|
|
85
97
|
*/
|
|
@@ -89,30 +101,30 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
89
101
|
* @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.
|
|
90
102
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
91
103
|
* be used.
|
|
92
|
-
* @returns The current query result. `status` is `pending` if there is no cached data
|
|
93
|
-
*
|
|
94
|
-
*
|
|
104
|
+
* @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if
|
|
105
|
+
* the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`
|
|
106
|
+
* are derived booleans for convenience.
|
|
95
107
|
*
|
|
96
108
|
* @example
|
|
97
109
|
* ```tsx
|
|
98
|
-
* import {
|
|
99
|
-
*
|
|
100
|
-
* const postsOptions = queryOptions({
|
|
101
|
-
* queryKey: ['posts'],
|
|
102
|
-
* queryFn: fetchPosts,
|
|
103
|
-
* })
|
|
110
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
104
111
|
*
|
|
105
112
|
* function Posts() {
|
|
106
|
-
* const { status, data, error, isFetching } = useQuery(
|
|
113
|
+
* const { status, data, error, isFetching } = useQuery({
|
|
114
|
+
* queryKey: ['posts'],
|
|
115
|
+
* queryFn: fetchPosts,
|
|
116
|
+
* })
|
|
107
117
|
*
|
|
108
118
|
* if (status === 'pending') return 'Loading...'
|
|
109
119
|
* if (status === 'error') return <span>Error: {error.message}</span>
|
|
110
120
|
*
|
|
111
121
|
* return (
|
|
112
122
|
* <div>
|
|
113
|
-
*
|
|
114
|
-
*
|
|
115
|
-
*
|
|
123
|
+
* <ul>
|
|
124
|
+
* {data.map((post) => (
|
|
125
|
+
* <li key={post.id}>{post.title}</li>
|
|
126
|
+
* ))}
|
|
127
|
+
* </ul>
|
|
116
128
|
* <div>{isFetching ? 'Background Updating...' : ' '}</div>
|
|
117
129
|
* </div>
|
|
118
130
|
* )
|
|
@@ -120,6 +132,26 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
120
132
|
* ```
|
|
121
133
|
*
|
|
122
134
|
* @example
|
|
135
|
+
* `select` derives whatever `data` a component needs from the cached value, without changing what's
|
|
136
|
+
* actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:
|
|
137
|
+
* ```tsx
|
|
138
|
+
* import { useQuery } from '@tanstack/preact-query'
|
|
139
|
+
*
|
|
140
|
+
* function PostCount() {
|
|
141
|
+
* const { data, isPending, isError, error } = useQuery({
|
|
142
|
+
* queryKey: ['posts'],
|
|
143
|
+
* queryFn: fetchPosts,
|
|
144
|
+
* select: (posts) => posts.length,
|
|
145
|
+
* })
|
|
146
|
+
*
|
|
147
|
+
* if (isPending) return 'Loading...'
|
|
148
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
149
|
+
*
|
|
150
|
+
* return <span>{data} posts</span>
|
|
151
|
+
* }
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* @example
|
|
123
155
|
* A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the
|
|
124
156
|
* loading state doesn't show while the query is disabled:
|
|
125
157
|
* ```tsx
|
|
@@ -141,6 +173,28 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
141
173
|
* ```
|
|
142
174
|
*
|
|
143
175
|
* @example
|
|
176
|
+
* The same dependent query, type safe: `skipToken` disables the query without needing the
|
|
177
|
+
* non-null assertion above, since `queryFn` is only ever called when `postId` is defined.
|
|
178
|
+
* `refetch` doesn't work while `queryFn` is `skipToken` — use `enabled: false` instead if you
|
|
179
|
+
* need to trigger the query manually:
|
|
180
|
+
* ```tsx
|
|
181
|
+
* import { skipToken, useQuery } from '@tanstack/preact-query'
|
|
182
|
+
*
|
|
183
|
+
* function Post({ postId }: { postId: number | undefined }) {
|
|
184
|
+
* const { data, isLoading, isError, error } = useQuery({
|
|
185
|
+
* queryKey: ['post', postId],
|
|
186
|
+
* queryFn: postId != null ? () => fetchPost(postId) : skipToken,
|
|
187
|
+
* })
|
|
188
|
+
*
|
|
189
|
+
* if (postId == null) return 'Select a post'
|
|
190
|
+
* if (isLoading) return 'Loading...'
|
|
191
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
192
|
+
*
|
|
193
|
+
* return <h1>{data?.title}</h1>
|
|
194
|
+
* }
|
|
195
|
+
* ```
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
144
198
|
* Seeding a detail query from an already-cached list, to skip the loading state:
|
|
145
199
|
* ```tsx
|
|
146
200
|
* import { useQuery, useQueryClient } from '@tanstack/preact-query'
|
|
@@ -148,7 +202,7 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
148
202
|
* function Post({ postId }: { postId: number }) {
|
|
149
203
|
* const queryClient = useQueryClient()
|
|
150
204
|
*
|
|
151
|
-
* const { data } = useQuery({
|
|
205
|
+
* const { data, isError, error } = useQuery({
|
|
152
206
|
* queryKey: ['post', postId],
|
|
153
207
|
* queryFn: () => fetchPost(postId),
|
|
154
208
|
* initialData: () =>
|
|
@@ -157,6 +211,8 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
157
211
|
* ?.find((post) => post.id === postId),
|
|
158
212
|
* })
|
|
159
213
|
*
|
|
214
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
215
|
+
*
|
|
160
216
|
* return <h1>{data?.title}</h1>
|
|
161
217
|
* }
|
|
162
218
|
* ```
|
|
@@ -170,15 +226,19 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
|
|
|
170
226
|
* function Posts() {
|
|
171
227
|
* const [page, setPage] = useState(0)
|
|
172
228
|
*
|
|
173
|
-
* const { data, isPlaceholderData } = useQuery({
|
|
229
|
+
* const { data, isPlaceholderData, isError, error } = useQuery({
|
|
174
230
|
* queryKey: ['posts', page],
|
|
175
231
|
* queryFn: () => fetchPosts(page),
|
|
176
232
|
* placeholderData: keepPreviousData,
|
|
177
233
|
* })
|
|
178
234
|
*
|
|
235
|
+
* if (isError) return <span>Error: {error.message}</span>
|
|
236
|
+
*
|
|
179
237
|
* return (
|
|
180
238
|
* <div>
|
|
181
|
-
*
|
|
239
|
+
* <ul>
|
|
240
|
+
* {data?.map((post) => <li key={post.id}>{post.title}</li>)}
|
|
241
|
+
* </ul>
|
|
182
242
|
* <button
|
|
183
243
|
* disabled={isPlaceholderData}
|
|
184
244
|
* onClick={() => setPage((old) => old + 1)}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.d.ts","names":[],"sources":["../../src/useQuery.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"useQuery.d.ts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiDgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA2DhB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4JT,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useQuery.js","names":[],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\n\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './queryOptions'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useBaseQuery } from './useBaseQuery'\n\n/**\n * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.\n *\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` set.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt\n * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,\n * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Posts() {\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData
|
|
1
|
+
{"version":3,"file":"useQuery.js","names":[],"sources":["../../src/useQuery.ts"],"sourcesContent":["import { QueryObserver } from '@tanstack/query-core'\nimport type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'\n\nimport type {\n DefinedInitialDataOptions,\n UndefinedInitialDataOptions,\n} from './queryOptions'\nimport type {\n DefinedUseQueryResult,\n UseQueryOptions,\n UseQueryResult,\n} from './types'\nimport { useBaseQuery } from './useBaseQuery'\n\n/**\n * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.\n *\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` set.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result, typed so that `status` is `success` — or `error` if a fetch attempt\n * fails while keeping the existing data (`status` never resolves to `pending` in this overload's type,\n * since `initialData` guarantees data upfront). `isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Posts() {\n * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,\n * // so the list stays visible alongside the error.\n * const { data, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n *\n * return (\n * <div>\n * {isError ? <span>Error: {error.message}</span> : null}\n * <ul>\n * {data.map((post) => <li key={post.id}>{post.title}</li>)}\n * </ul>\n * </div>\n * )\n * }\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): DefinedUseQueryResult<TData, TError>\n\n/**\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if\n * the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`\n * are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * <ul>\n * {data.map((post) => (\n * <li key={post.id}>{post.title}</li>\n * ))}\n * </ul>\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * The same query, checking `isPending`/`isError` instead of `status` — pick whichever reads better to you:\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Posts() {\n * const { isPending, isError, data, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * if (isPending) return 'Loading...'\n * if (isError) return <span>Error: {error.message}</span>\n *\n * return (\n * <ul>\n * {data.map((post) => <li key={post.id}>{post.title}</li>)}\n * </ul>\n * )\n * }\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryResult<TData, TError>\n\n/**\n * @see {@link queryOptions} to share these options between `useQuery` and imperative APIs like `queryClient.query`.\n * @param options - The {@link UseQueryOptions} to use — everything you can pass to `useQuery`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The current query result. `status` is `pending` if there is no cached data to display, `error` if\n * the last fetch attempt failed, or `success` if the query has data to display. `isPending`/`isSuccess`/`isError`\n * are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * <ul>\n * {data.map((post) => (\n * <li key={post.id}>{post.title}</li>\n * ))}\n * </ul>\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * `select` derives whatever `data` a component needs from the cached value, without changing what's\n * actually stored in the cache — the cache still holds the full `Post[]`, but `data` here is a `number`:\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function PostCount() {\n * const { data, isPending, isError, error } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * select: (posts) => posts.length,\n * })\n *\n * if (isPending) return 'Loading...'\n * if (isError) return <span>Error: {error.message}</span>\n *\n * return <span>{data} posts</span>\n * }\n * ```\n *\n * @example\n * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the\n * loading state doesn't show while the query is disabled:\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number | undefined }) {\n * const { data, isLoading, isError, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId!),\n * enabled: postId != null,\n * })\n *\n * if (postId == null) return 'Select a post'\n * if (isLoading) return 'Loading...'\n * if (isError) return <span>Error: {error.message}</span>\n *\n * return <h1>{data?.title}</h1>\n * }\n * ```\n *\n * @example\n * The same dependent query, type safe: `skipToken` disables the query without needing the\n * non-null assertion above, since `queryFn` is only ever called when `postId` is defined.\n * `refetch` doesn't work while `queryFn` is `skipToken` — use `enabled: false` instead if you\n * need to trigger the query manually:\n * ```tsx\n * import { skipToken, useQuery } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number | undefined }) {\n * const { data, isLoading, isError, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: postId != null ? () => fetchPost(postId) : skipToken,\n * })\n *\n * if (postId == null) return 'Select a post'\n * if (isLoading) return 'Loading...'\n * if (isError) return <span>Error: {error.message}</span>\n *\n * return <h1>{data?.title}</h1>\n * }\n * ```\n *\n * @example\n * Seeding a detail query from an already-cached list, to skip the loading state:\n * ```tsx\n * import { useQuery, useQueryClient } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number }) {\n * const queryClient = useQueryClient()\n *\n * const { data, isError, error } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId),\n * initialData: () =>\n * queryClient\n * .getQueryData<Array<Post>>(['posts'])\n * ?.find((post) => post.id === postId),\n * })\n *\n * if (isError) return <span>Error: {error.message}</span>\n *\n * return <h1>{data?.title}</h1>\n * }\n * ```\n *\n * @example\n * Paginated data, keeping the previous page's data visible while the next page loads:\n * ```tsx\n * import { keepPreviousData, useQuery } from '@tanstack/preact-query'\n * import { useState } from 'preact/hooks'\n *\n * function Posts() {\n * const [page, setPage] = useState(0)\n *\n * const { data, isPlaceholderData, isError, error } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page),\n * placeholderData: keepPreviousData,\n * })\n *\n * if (isError) return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * <ul>\n * {data?.map((post) => <li key={post.id}>{post.title}</li>)}\n * </ul>\n * <button\n * disabled={isPlaceholderData}\n * onClick={() => setPage((old) => old + 1)}\n * >\n * Next Page\n * </button>\n * </div>\n * )\n * }\n * ```\n */\nexport function useQuery<\n TQueryFnData = unknown,\n TError = DefaultError,\n TData = TQueryFnData,\n TQueryKey extends QueryKey = QueryKey,\n>(\n options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,\n queryClient?: QueryClient,\n): UseQueryResult<TData, TError>\n\nexport function useQuery(options: UseQueryOptions, queryClient?: QueryClient) {\n return useBaseQuery(options, QueryObserver, queryClient)\n}\n"],"mappings":";;;AAkSA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAO,aAAa,SAAS,eAAe,WAAW;AACzD"}
|
|
@@ -9,6 +9,13 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
9
9
|
*
|
|
10
10
|
* Caveat: cancellation does not work.
|
|
11
11
|
*
|
|
12
|
+
* @remarks Multiple suspenseful query calls in the same component suspend serially, causing a request
|
|
13
|
+
* waterfall — each one blocks rendering until it resolves, so the next doesn't even start fetching until
|
|
14
|
+
* then. There's no way to parallelize multiple infinite queries under Suspense. Also keep in mind that
|
|
15
|
+
* imperative fetch calls, such as `fetchNextPage`, may interfere with the default refetch behavior,
|
|
16
|
+
* resulting in outdated data. Make sure to call these functions only in response to user actions, or add
|
|
17
|
+
* conditions like `hasNextPage && !isFetching`.
|
|
18
|
+
* @see {@link useInfiniteQuery} for the non-Suspense version of this hook.
|
|
12
19
|
* @param options - The {@link UseSuspenseInfiniteQueryOptions} to use — the same options as `useInfiniteQuery`, minus the ones listed above.
|
|
13
20
|
* @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
|
|
14
21
|
* be used.
|
|
@@ -17,9 +24,17 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
17
24
|
* accordingly).
|
|
18
25
|
*
|
|
19
26
|
* @example
|
|
27
|
+
* The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is
|
|
28
|
+
* required around `<Suspense>`. A failed background refetch instead continues to render the cached data.
|
|
29
|
+
* Use {@link QueryErrorResetBoundary} to let the user retry after such an error:
|
|
20
30
|
* ```tsx
|
|
21
31
|
* import { Suspense } from 'preact/compat'
|
|
22
|
-
* import {
|
|
32
|
+
* import { useErrorBoundary } from 'preact/hooks'
|
|
33
|
+
* import {
|
|
34
|
+
* QueryErrorResetBoundary,
|
|
35
|
+
* useSuspenseInfiniteQuery,
|
|
36
|
+
* } from '@tanstack/preact-query'
|
|
37
|
+
* import type { ComponentChildren } from 'preact'
|
|
23
38
|
*
|
|
24
39
|
* function Projects() {
|
|
25
40
|
* // `data` is guaranteed to be defined here — no `isPending` check needed.
|
|
@@ -33,9 +48,11 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
33
48
|
*
|
|
34
49
|
* return (
|
|
35
50
|
* <div>
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
51
|
+
* <ul>
|
|
52
|
+
* {data.pages.map((page) =>
|
|
53
|
+
* page.projects.map((project) => <li key={project.id}>{project.name}</li>),
|
|
54
|
+
* )}
|
|
55
|
+
* </ul>
|
|
39
56
|
* <button
|
|
40
57
|
* onClick={() => fetchNextPage()}
|
|
41
58
|
* disabled={!hasNextPage || isFetching}
|
|
@@ -52,11 +69,44 @@ let _tanstack_query_core = require("@tanstack/query-core");
|
|
|
52
69
|
*
|
|
53
70
|
* function App() {
|
|
54
71
|
* return (
|
|
55
|
-
* <
|
|
56
|
-
*
|
|
57
|
-
*
|
|
72
|
+
* <QueryErrorResetBoundary>
|
|
73
|
+
* {({ reset }) => (
|
|
74
|
+
* <ErrorBoundary
|
|
75
|
+
* onReset={reset}
|
|
76
|
+
* fallbackRender={({ resetErrorBoundary }) => (
|
|
77
|
+
* <div>
|
|
78
|
+
* There was an error!
|
|
79
|
+
* <button onClick={() => resetErrorBoundary()}>Try again</button>
|
|
80
|
+
* </div>
|
|
81
|
+
* )}
|
|
82
|
+
* >
|
|
83
|
+
* <Suspense fallback={<h1>Loading projects...</h1>}>
|
|
84
|
+
* <Projects />
|
|
85
|
+
* </Suspense>
|
|
86
|
+
* </ErrorBoundary>
|
|
87
|
+
* )}
|
|
88
|
+
* </QueryErrorResetBoundary>
|
|
58
89
|
* )
|
|
59
90
|
* }
|
|
91
|
+
*
|
|
92
|
+
* function ErrorBoundary({
|
|
93
|
+
* children,
|
|
94
|
+
* onReset,
|
|
95
|
+
* fallbackRender,
|
|
96
|
+
* }: {
|
|
97
|
+
* children: ComponentChildren
|
|
98
|
+
* onReset: () => void
|
|
99
|
+
* fallbackRender: (props: {
|
|
100
|
+
* error: Error
|
|
101
|
+
* resetErrorBoundary: () => void
|
|
102
|
+
* }) => ComponentChildren
|
|
103
|
+
* }) {
|
|
104
|
+
* const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())
|
|
105
|
+
*
|
|
106
|
+
* if (error) return fallbackRender({ error, resetErrorBoundary })
|
|
107
|
+
*
|
|
108
|
+
* return children
|
|
109
|
+
* }
|
|
60
110
|
* ```
|
|
61
111
|
*/
|
|
62
112
|
function useSuspenseInfiniteQuery(options, queryClient) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useSuspenseInfiniteQuery.cjs","names":["skipToken","useBaseQuery","defaultThrowOnError","InfiniteQueryObserver"],"sources":["../../src/useSuspenseInfiniteQuery.ts"],"sourcesContent":["import { InfiniteQueryObserver, skipToken } from '@tanstack/query-core'\nimport type {\n DefaultError,\n InfiniteData,\n InfiniteQueryObserverSuccessResult,\n QueryClient,\n QueryKey,\n QueryObserver,\n} from '@tanstack/query-core'\n\nimport { defaultThrowOnError } from './suspense'\nimport type {\n UseSuspenseInfiniteQueryOptions,\n UseSuspenseInfiniteQueryResult,\n} from './types'\nimport { useBaseQuery } from './useBaseQuery'\n\n/**\n * The options for `useSuspenseInfiniteQuery` are the same as for `useInfiniteQuery`, except for `throwOnError`,\n * `enabled`, and `placeholderData`.\n *\n * Caveat: cancellation does not work.\n *\n * @param options - The {@link UseSuspenseInfiniteQueryOptions} to use — the same options as `useInfiniteQuery`, minus the ones listed above.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The same object as `useInfiniteQuery`, except that `data` is guaranteed to be defined,\n * `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived flags set\n * accordingly).\n *\n * @example\n * ```tsx\n * import { Suspense } from 'preact/compat'\n * import { useSuspenseInfiniteQuery } from '@tanstack/preact-query'\n *\n * function Projects() {\n * // `data` is guaranteed to be defined here — no `isPending` check needed.\n * const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =\n * useSuspenseInfiniteQuery({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * return (\n * <div>\n * {data.pages.map((page) =>\n *
|
|
1
|
+
{"version":3,"file":"useSuspenseInfiniteQuery.cjs","names":["skipToken","useBaseQuery","defaultThrowOnError","InfiniteQueryObserver"],"sources":["../../src/useSuspenseInfiniteQuery.ts"],"sourcesContent":["import { InfiniteQueryObserver, skipToken } from '@tanstack/query-core'\nimport type {\n DefaultError,\n InfiniteData,\n InfiniteQueryObserverSuccessResult,\n QueryClient,\n QueryKey,\n QueryObserver,\n} from '@tanstack/query-core'\n\nimport { defaultThrowOnError } from './suspense'\nimport type {\n UseSuspenseInfiniteQueryOptions,\n UseSuspenseInfiniteQueryResult,\n} from './types'\nimport { useBaseQuery } from './useBaseQuery'\n\n/**\n * The options for `useSuspenseInfiniteQuery` are the same as for `useInfiniteQuery`, except for `throwOnError`,\n * `enabled`, and `placeholderData`.\n *\n * Caveat: cancellation does not work.\n *\n * @remarks Multiple suspenseful query calls in the same component suspend serially, causing a request\n * waterfall — each one blocks rendering until it resolves, so the next doesn't even start fetching until\n * then. There's no way to parallelize multiple infinite queries under Suspense. Also keep in mind that\n * imperative fetch calls, such as `fetchNextPage`, may interfere with the default refetch behavior,\n * resulting in outdated data. Make sure to call these functions only in response to user actions, or add\n * conditions like `hasNextPage && !isFetching`.\n * @see {@link useInfiniteQuery} for the non-Suspense version of this hook.\n * @param options - The {@link UseSuspenseInfiniteQueryOptions} to use — the same options as `useInfiniteQuery`, minus the ones listed above.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns The same object as `useInfiniteQuery`, except that `data` is guaranteed to be defined,\n * `isPlaceholderData` is missing, and `status` is either `success` or `error` (with the derived flags set\n * accordingly).\n *\n * @example\n * The query error is thrown if a fetch fails and no cached data exists yet, so an error boundary is\n * required around `<Suspense>`. A failed background refetch instead continues to render the cached data.\n * Use {@link QueryErrorResetBoundary} to let the user retry after such an error:\n * ```tsx\n * import { Suspense } from 'preact/compat'\n * import { useErrorBoundary } from 'preact/hooks'\n * import {\n * QueryErrorResetBoundary,\n * useSuspenseInfiniteQuery,\n * } from '@tanstack/preact-query'\n * import type { ComponentChildren } from 'preact'\n *\n * function Projects() {\n * // `data` is guaranteed to be defined here — no `isPending` check needed.\n * const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =\n * useSuspenseInfiniteQuery({\n * queryKey: ['projects'],\n * queryFn: ({ pageParam }) => fetchProjects(pageParam),\n * initialPageParam: 0,\n * getNextPageParam: (lastPage) => lastPage.nextId,\n * })\n *\n * return (\n * <div>\n * <ul>\n * {data.pages.map((page) =>\n * page.projects.map((project) => <li key={project.id}>{project.name}</li>),\n * )}\n * </ul>\n * <button\n * onClick={() => fetchNextPage()}\n * disabled={!hasNextPage || isFetching}\n * >\n * {isFetchingNextPage\n * ? 'Loading more...'\n * : hasNextPage\n * ? 'Load More'\n * : 'Nothing more to load'}\n * </button>\n * </div>\n * )\n * }\n *\n * function App() {\n * return (\n * <QueryErrorResetBoundary>\n * {({ reset }) => (\n * <ErrorBoundary\n * onReset={reset}\n * fallbackRender={({ resetErrorBoundary }) => (\n * <div>\n * There was an error!\n * <button onClick={() => resetErrorBoundary()}>Try again</button>\n * </div>\n * )}\n * >\n * <Suspense fallback={<h1>Loading projects...</h1>}>\n * <Projects />\n * </Suspense>\n * </ErrorBoundary>\n * )}\n * </QueryErrorResetBoundary>\n * )\n * }\n *\n * function ErrorBoundary({\n * children,\n * onReset,\n * fallbackRender,\n * }: {\n * children: ComponentChildren\n * onReset: () => void\n * fallbackRender: (props: {\n * error: Error\n * resetErrorBoundary: () => void\n * }) => ComponentChildren\n * }) {\n * const [error, resetErrorBoundary] = useErrorBoundary(() => onReset())\n *\n * if (error) return fallbackRender({ error, resetErrorBoundary })\n *\n * return children\n * }\n * ```\n */\nexport function useSuspenseInfiniteQuery<\n TQueryFnData,\n TError = DefaultError,\n TData = InfiniteData<TQueryFnData>,\n TQueryKey extends QueryKey = QueryKey,\n TPageParam = unknown,\n>(\n options: UseSuspenseInfiniteQueryOptions<\n TQueryFnData,\n TError,\n TData,\n TQueryKey,\n TPageParam\n >,\n queryClient?: QueryClient,\n): UseSuspenseInfiniteQueryResult<TData, TError> {\n if (process.env.NODE_ENV !== 'production') {\n if ((options.queryFn as any) === skipToken) {\n console.error('skipToken is not allowed for useSuspenseInfiniteQuery')\n }\n }\n\n return useBaseQuery(\n {\n ...options,\n enabled: true,\n suspense: true,\n throwOnError: defaultThrowOnError,\n placeholderData: undefined,\n },\n InfiniteQueryObserver as typeof QueryObserver,\n queryClient,\n ) as InfiniteQueryObserverSuccessResult<TData, TError>\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2HA,SAAgB,yBAOd,SAOA,aAC+C;CAC/C,IAAI,QAAQ,IAAI,aAAa,cACtB;MAAA,QAAQ,YAAoBA,qBAAAA,WAC/B,QAAQ,MAAM,uDAAuD;CAAA;CAIzE,OAAOC,qBAAAA,aACL;EACE,GAAG;EACH,SAAS;EACT,UAAU;EACV,cAAcC,iBAAAA;EACd,iBAAiB,KAAA;CACnB,GACAC,qBAAAA,uBACA,WACF;AACF"}
|