@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.
Files changed (305) hide show
  1. package/build/legacy/HydrationBoundary.cjs +43 -0
  2. package/build/legacy/HydrationBoundary.cjs.map +1 -1
  3. package/build/legacy/HydrationBoundary.d.cts +61 -0
  4. package/build/legacy/HydrationBoundary.d.cts.map +1 -1
  5. package/build/legacy/HydrationBoundary.d.ts +61 -0
  6. package/build/legacy/HydrationBoundary.d.ts.map +1 -1
  7. package/build/legacy/HydrationBoundary.js +43 -0
  8. package/build/legacy/HydrationBoundary.js.map +1 -1
  9. package/build/legacy/IsRestoringProvider.cjs +11 -0
  10. package/build/legacy/IsRestoringProvider.cjs.map +1 -1
  11. package/build/legacy/IsRestoringProvider.d.cts +11 -0
  12. package/build/legacy/IsRestoringProvider.d.cts.map +1 -1
  13. package/build/legacy/IsRestoringProvider.d.ts +11 -0
  14. package/build/legacy/IsRestoringProvider.d.ts.map +1 -1
  15. package/build/legacy/IsRestoringProvider.js +11 -0
  16. package/build/legacy/IsRestoringProvider.js.map +1 -1
  17. package/build/legacy/QueryClientProvider.cjs +30 -0
  18. package/build/legacy/QueryClientProvider.cjs.map +1 -1
  19. package/build/legacy/QueryClientProvider.d.cts +41 -0
  20. package/build/legacy/QueryClientProvider.d.cts.map +1 -1
  21. package/build/legacy/QueryClientProvider.d.ts +41 -0
  22. package/build/legacy/QueryClientProvider.d.ts.map +1 -1
  23. package/build/legacy/QueryClientProvider.js +30 -0
  24. package/build/legacy/QueryClientProvider.js.map +1 -1
  25. package/build/legacy/QueryErrorResetBoundary.cjs +75 -0
  26. package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
  27. package/build/legacy/QueryErrorResetBoundary.d.cts +106 -0
  28. package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
  29. package/build/legacy/QueryErrorResetBoundary.d.ts +106 -0
  30. package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
  31. package/build/legacy/QueryErrorResetBoundary.js +75 -0
  32. package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
  33. package/build/legacy/infiniteQueryOptions.cjs.map +1 -1
  34. package/build/legacy/infiniteQueryOptions.d.cts +187 -0
  35. package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
  36. package/build/legacy/infiniteQueryOptions.d.ts +187 -0
  37. package/build/legacy/infiniteQueryOptions.d.ts.map +1 -1
  38. package/build/legacy/infiniteQueryOptions.js.map +1 -1
  39. package/build/legacy/mutationOptions.cjs.map +1 -1
  40. package/build/legacy/mutationOptions.d.cts +68 -0
  41. package/build/legacy/mutationOptions.d.cts.map +1 -1
  42. package/build/legacy/mutationOptions.d.ts +68 -0
  43. package/build/legacy/mutationOptions.d.ts.map +1 -1
  44. package/build/legacy/mutationOptions.js.map +1 -1
  45. package/build/legacy/queryOptions.cjs.map +1 -1
  46. package/build/legacy/queryOptions.d.cts +198 -0
  47. package/build/legacy/queryOptions.d.cts.map +1 -1
  48. package/build/legacy/queryOptions.d.ts +198 -0
  49. package/build/legacy/queryOptions.d.ts.map +1 -1
  50. package/build/legacy/queryOptions.js.map +1 -1
  51. package/build/legacy/types.cjs.map +1 -1
  52. package/build/legacy/types.d.cts +228 -2
  53. package/build/legacy/types.d.cts.map +1 -1
  54. package/build/legacy/types.d.ts +228 -2
  55. package/build/legacy/types.d.ts.map +1 -1
  56. package/build/legacy/types.js.map +1 -1
  57. package/build/legacy/useInfiniteQuery.cjs.map +1 -1
  58. package/build/legacy/useInfiniteQuery.d.cts +116 -0
  59. package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
  60. package/build/legacy/useInfiniteQuery.d.ts +116 -0
  61. package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
  62. package/build/legacy/useInfiniteQuery.js.map +1 -1
  63. package/build/legacy/useIsFetching.cjs +20 -0
  64. package/build/legacy/useIsFetching.cjs.map +1 -1
  65. package/build/legacy/useIsFetching.d.cts +20 -0
  66. package/build/legacy/useIsFetching.d.cts.map +1 -1
  67. package/build/legacy/useIsFetching.d.ts +20 -0
  68. package/build/legacy/useIsFetching.d.ts.map +1 -1
  69. package/build/legacy/useIsFetching.js +20 -0
  70. package/build/legacy/useIsFetching.js.map +1 -1
  71. package/build/legacy/useMutation.cjs +67 -0
  72. package/build/legacy/useMutation.cjs.map +1 -1
  73. package/build/legacy/useMutation.d.cts +67 -0
  74. package/build/legacy/useMutation.d.cts.map +1 -1
  75. package/build/legacy/useMutation.d.ts +67 -0
  76. package/build/legacy/useMutation.d.ts.map +1 -1
  77. package/build/legacy/useMutation.js +67 -0
  78. package/build/legacy/useMutation.js.map +1 -1
  79. package/build/legacy/useMutationState.cjs +74 -0
  80. package/build/legacy/useMutationState.cjs.map +1 -1
  81. package/build/legacy/useMutationState.d.cts +74 -0
  82. package/build/legacy/useMutationState.d.cts.map +1 -1
  83. package/build/legacy/useMutationState.d.ts +74 -0
  84. package/build/legacy/useMutationState.d.ts.map +1 -1
  85. package/build/legacy/useMutationState.js +74 -0
  86. package/build/legacy/useMutationState.js.map +1 -1
  87. package/build/legacy/usePrefetchInfiniteQuery.cjs +44 -0
  88. package/build/legacy/usePrefetchInfiniteQuery.cjs.map +1 -1
  89. package/build/legacy/usePrefetchInfiniteQuery.d.cts +44 -0
  90. package/build/legacy/usePrefetchInfiniteQuery.d.cts.map +1 -1
  91. package/build/legacy/usePrefetchInfiniteQuery.d.ts +44 -0
  92. package/build/legacy/usePrefetchInfiniteQuery.d.ts.map +1 -1
  93. package/build/legacy/usePrefetchInfiniteQuery.js +44 -0
  94. package/build/legacy/usePrefetchInfiniteQuery.js.map +1 -1
  95. package/build/legacy/usePrefetchQuery.cjs +36 -1
  96. package/build/legacy/usePrefetchQuery.cjs.map +1 -1
  97. package/build/legacy/usePrefetchQuery.d.cts +35 -0
  98. package/build/legacy/usePrefetchQuery.d.cts.map +1 -1
  99. package/build/legacy/usePrefetchQuery.d.ts +35 -0
  100. package/build/legacy/usePrefetchQuery.d.ts.map +1 -1
  101. package/build/legacy/usePrefetchQuery.js +36 -1
  102. package/build/legacy/usePrefetchQuery.js.map +1 -1
  103. package/build/legacy/useQueries.cjs +50 -0
  104. package/build/legacy/useQueries.cjs.map +1 -1
  105. package/build/legacy/useQueries.d.cts +85 -2
  106. package/build/legacy/useQueries.d.cts.map +1 -1
  107. package/build/legacy/useQueries.d.ts +85 -2
  108. package/build/legacy/useQueries.d.ts.map +1 -1
  109. package/build/legacy/useQueries.js +50 -0
  110. package/build/legacy/useQueries.js.map +1 -1
  111. package/build/legacy/useQuery.cjs.map +1 -1
  112. package/build/legacy/useQuery.d.cts +134 -0
  113. package/build/legacy/useQuery.d.cts.map +1 -1
  114. package/build/legacy/useQuery.d.ts +134 -0
  115. package/build/legacy/useQuery.d.ts.map +1 -1
  116. package/build/legacy/useQuery.js.map +1 -1
  117. package/build/legacy/useSuspenseInfiniteQuery.cjs +48 -0
  118. package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
  119. package/build/legacy/useSuspenseInfiniteQuery.d.cts +48 -0
  120. package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
  121. package/build/legacy/useSuspenseInfiniteQuery.d.ts +48 -0
  122. package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
  123. package/build/legacy/useSuspenseInfiniteQuery.js +48 -0
  124. package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
  125. package/build/legacy/useSuspenseQueries.cjs.map +1 -1
  126. package/build/legacy/useSuspenseQueries.d.cts +128 -2
  127. package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
  128. package/build/legacy/useSuspenseQueries.d.ts +128 -2
  129. package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
  130. package/build/legacy/useSuspenseQueries.js.map +1 -1
  131. package/build/legacy/useSuspenseQuery.cjs +43 -0
  132. package/build/legacy/useSuspenseQuery.cjs.map +1 -1
  133. package/build/legacy/useSuspenseQuery.d.cts +43 -0
  134. package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
  135. package/build/legacy/useSuspenseQuery.d.ts +43 -0
  136. package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
  137. package/build/legacy/useSuspenseQuery.js +43 -0
  138. package/build/legacy/useSuspenseQuery.js.map +1 -1
  139. package/build/legacy/utils.cjs.map +1 -1
  140. package/build/legacy/utils.d.cts.map +1 -1
  141. package/build/legacy/utils.d.ts.map +1 -1
  142. package/build/legacy/utils.js.map +1 -1
  143. package/build/modern/HydrationBoundary.cjs +43 -0
  144. package/build/modern/HydrationBoundary.cjs.map +1 -1
  145. package/build/modern/HydrationBoundary.d.cts +61 -0
  146. package/build/modern/HydrationBoundary.d.cts.map +1 -1
  147. package/build/modern/HydrationBoundary.d.ts +61 -0
  148. package/build/modern/HydrationBoundary.d.ts.map +1 -1
  149. package/build/modern/HydrationBoundary.js +43 -0
  150. package/build/modern/HydrationBoundary.js.map +1 -1
  151. package/build/modern/IsRestoringProvider.cjs +11 -0
  152. package/build/modern/IsRestoringProvider.cjs.map +1 -1
  153. package/build/modern/IsRestoringProvider.d.cts +11 -0
  154. package/build/modern/IsRestoringProvider.d.cts.map +1 -1
  155. package/build/modern/IsRestoringProvider.d.ts +11 -0
  156. package/build/modern/IsRestoringProvider.d.ts.map +1 -1
  157. package/build/modern/IsRestoringProvider.js +11 -0
  158. package/build/modern/IsRestoringProvider.js.map +1 -1
  159. package/build/modern/QueryClientProvider.cjs +30 -0
  160. package/build/modern/QueryClientProvider.cjs.map +1 -1
  161. package/build/modern/QueryClientProvider.d.cts +41 -0
  162. package/build/modern/QueryClientProvider.d.cts.map +1 -1
  163. package/build/modern/QueryClientProvider.d.ts +41 -0
  164. package/build/modern/QueryClientProvider.d.ts.map +1 -1
  165. package/build/modern/QueryClientProvider.js +30 -0
  166. package/build/modern/QueryClientProvider.js.map +1 -1
  167. package/build/modern/QueryErrorResetBoundary.cjs +75 -0
  168. package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
  169. package/build/modern/QueryErrorResetBoundary.d.cts +106 -0
  170. package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
  171. package/build/modern/QueryErrorResetBoundary.d.ts +106 -0
  172. package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
  173. package/build/modern/QueryErrorResetBoundary.js +75 -0
  174. package/build/modern/QueryErrorResetBoundary.js.map +1 -1
  175. package/build/modern/infiniteQueryOptions.cjs.map +1 -1
  176. package/build/modern/infiniteQueryOptions.d.cts +187 -0
  177. package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
  178. package/build/modern/infiniteQueryOptions.d.ts +187 -0
  179. package/build/modern/infiniteQueryOptions.d.ts.map +1 -1
  180. package/build/modern/infiniteQueryOptions.js.map +1 -1
  181. package/build/modern/mutationOptions.cjs.map +1 -1
  182. package/build/modern/mutationOptions.d.cts +68 -0
  183. package/build/modern/mutationOptions.d.cts.map +1 -1
  184. package/build/modern/mutationOptions.d.ts +68 -0
  185. package/build/modern/mutationOptions.d.ts.map +1 -1
  186. package/build/modern/mutationOptions.js.map +1 -1
  187. package/build/modern/queryOptions.cjs.map +1 -1
  188. package/build/modern/queryOptions.d.cts +198 -0
  189. package/build/modern/queryOptions.d.cts.map +1 -1
  190. package/build/modern/queryOptions.d.ts +198 -0
  191. package/build/modern/queryOptions.d.ts.map +1 -1
  192. package/build/modern/queryOptions.js.map +1 -1
  193. package/build/modern/types.cjs.map +1 -1
  194. package/build/modern/types.d.cts +228 -2
  195. package/build/modern/types.d.cts.map +1 -1
  196. package/build/modern/types.d.ts +228 -2
  197. package/build/modern/types.d.ts.map +1 -1
  198. package/build/modern/types.js.map +1 -1
  199. package/build/modern/useInfiniteQuery.cjs.map +1 -1
  200. package/build/modern/useInfiniteQuery.d.cts +116 -0
  201. package/build/modern/useInfiniteQuery.d.cts.map +1 -1
  202. package/build/modern/useInfiniteQuery.d.ts +116 -0
  203. package/build/modern/useInfiniteQuery.d.ts.map +1 -1
  204. package/build/modern/useInfiniteQuery.js.map +1 -1
  205. package/build/modern/useIsFetching.cjs +20 -0
  206. package/build/modern/useIsFetching.cjs.map +1 -1
  207. package/build/modern/useIsFetching.d.cts +20 -0
  208. package/build/modern/useIsFetching.d.cts.map +1 -1
  209. package/build/modern/useIsFetching.d.ts +20 -0
  210. package/build/modern/useIsFetching.d.ts.map +1 -1
  211. package/build/modern/useIsFetching.js +20 -0
  212. package/build/modern/useIsFetching.js.map +1 -1
  213. package/build/modern/useMutation.cjs +67 -0
  214. package/build/modern/useMutation.cjs.map +1 -1
  215. package/build/modern/useMutation.d.cts +67 -0
  216. package/build/modern/useMutation.d.cts.map +1 -1
  217. package/build/modern/useMutation.d.ts +67 -0
  218. package/build/modern/useMutation.d.ts.map +1 -1
  219. package/build/modern/useMutation.js +67 -0
  220. package/build/modern/useMutation.js.map +1 -1
  221. package/build/modern/useMutationState.cjs +74 -0
  222. package/build/modern/useMutationState.cjs.map +1 -1
  223. package/build/modern/useMutationState.d.cts +74 -0
  224. package/build/modern/useMutationState.d.cts.map +1 -1
  225. package/build/modern/useMutationState.d.ts +74 -0
  226. package/build/modern/useMutationState.d.ts.map +1 -1
  227. package/build/modern/useMutationState.js +74 -0
  228. package/build/modern/useMutationState.js.map +1 -1
  229. package/build/modern/usePrefetchInfiniteQuery.cjs +44 -0
  230. package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
  231. package/build/modern/usePrefetchInfiniteQuery.d.cts +44 -0
  232. package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
  233. package/build/modern/usePrefetchInfiniteQuery.d.ts +44 -0
  234. package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
  235. package/build/modern/usePrefetchInfiniteQuery.js +44 -0
  236. package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
  237. package/build/modern/usePrefetchQuery.cjs +36 -1
  238. package/build/modern/usePrefetchQuery.cjs.map +1 -1
  239. package/build/modern/usePrefetchQuery.d.cts +35 -0
  240. package/build/modern/usePrefetchQuery.d.cts.map +1 -1
  241. package/build/modern/usePrefetchQuery.d.ts +35 -0
  242. package/build/modern/usePrefetchQuery.d.ts.map +1 -1
  243. package/build/modern/usePrefetchQuery.js +36 -1
  244. package/build/modern/usePrefetchQuery.js.map +1 -1
  245. package/build/modern/useQueries.cjs +50 -0
  246. package/build/modern/useQueries.cjs.map +1 -1
  247. package/build/modern/useQueries.d.cts +85 -2
  248. package/build/modern/useQueries.d.cts.map +1 -1
  249. package/build/modern/useQueries.d.ts +85 -2
  250. package/build/modern/useQueries.d.ts.map +1 -1
  251. package/build/modern/useQueries.js +50 -0
  252. package/build/modern/useQueries.js.map +1 -1
  253. package/build/modern/useQuery.cjs.map +1 -1
  254. package/build/modern/useQuery.d.cts +134 -0
  255. package/build/modern/useQuery.d.cts.map +1 -1
  256. package/build/modern/useQuery.d.ts +134 -0
  257. package/build/modern/useQuery.d.ts.map +1 -1
  258. package/build/modern/useQuery.js.map +1 -1
  259. package/build/modern/useSuspenseInfiniteQuery.cjs +48 -0
  260. package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
  261. package/build/modern/useSuspenseInfiniteQuery.d.cts +48 -0
  262. package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
  263. package/build/modern/useSuspenseInfiniteQuery.d.ts +48 -0
  264. package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
  265. package/build/modern/useSuspenseInfiniteQuery.js +48 -0
  266. package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
  267. package/build/modern/useSuspenseQueries.cjs.map +1 -1
  268. package/build/modern/useSuspenseQueries.d.cts +128 -2
  269. package/build/modern/useSuspenseQueries.d.cts.map +1 -1
  270. package/build/modern/useSuspenseQueries.d.ts +128 -2
  271. package/build/modern/useSuspenseQueries.d.ts.map +1 -1
  272. package/build/modern/useSuspenseQueries.js.map +1 -1
  273. package/build/modern/useSuspenseQuery.cjs +43 -0
  274. package/build/modern/useSuspenseQuery.cjs.map +1 -1
  275. package/build/modern/useSuspenseQuery.d.cts +43 -0
  276. package/build/modern/useSuspenseQuery.d.cts.map +1 -1
  277. package/build/modern/useSuspenseQuery.d.ts +43 -0
  278. package/build/modern/useSuspenseQuery.d.ts.map +1 -1
  279. package/build/modern/useSuspenseQuery.js +43 -0
  280. package/build/modern/useSuspenseQuery.js.map +1 -1
  281. package/build/modern/utils.cjs.map +1 -1
  282. package/build/modern/utils.d.cts.map +1 -1
  283. package/build/modern/utils.d.ts.map +1 -1
  284. package/build/modern/utils.js.map +1 -1
  285. package/package.json +3 -3
  286. package/src/HydrationBoundary.tsx +61 -1
  287. package/src/IsRestoringProvider.ts +12 -0
  288. package/src/QueryClientProvider.tsx +41 -0
  289. package/src/QueryErrorResetBoundary.tsx +109 -0
  290. package/src/infiniteQueryOptions.ts +187 -0
  291. package/src/mutationOptions.ts +68 -0
  292. package/src/queryOptions.ts +198 -0
  293. package/src/types.ts +228 -2
  294. package/src/useInfiniteQuery.ts +116 -0
  295. package/src/useIsFetching.ts +20 -0
  296. package/src/useMutation.ts +67 -0
  297. package/src/useMutationState.ts +74 -0
  298. package/src/usePrefetchInfiniteQuery.tsx +44 -0
  299. package/src/usePrefetchQuery.tsx +36 -1
  300. package/src/useQueries.ts +85 -2
  301. package/src/useQuery.ts +134 -0
  302. package/src/useSuspenseInfiniteQuery.ts +48 -0
  303. package/src/useSuspenseQueries.ts +128 -2
  304. package/src/useSuspenseQuery.ts +43 -0
  305. package/src/utils.ts +0 -1
@@ -18,6 +18,40 @@ import type {
18
18
  } from './types'
19
19
  import { useBaseQuery } from './useBaseQuery'
20
20
 
21
+ /**
22
+ * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of `queryFn`,
23
+ * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
24
+ *
25
+ * This overload is selected when `initialData` is set.
26
+ *
27
+ * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
28
+ * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
29
+ * actions, or add conditions like `hasNextPage && !isFetching`.
30
+ * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
31
+ * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` set.
32
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
33
+ * be used.
34
+ * @returns The same properties as `useQuery`, with the addition of `data.pages`, `data.pageParams`,
35
+ * `fetchNextPage`, `fetchPreviousPage`, `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and
36
+ * `isFetchingPreviousPage`.
37
+ *
38
+ * @example
39
+ * ```tsx
40
+ * import { useInfiniteQuery } from '@tanstack/preact-query'
41
+ *
42
+ * function Projects() {
43
+ * const { data } = useInfiniteQuery({
44
+ * queryKey: ['projects'],
45
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
46
+ * initialPageParam: 0,
47
+ * getNextPageParam: (lastPage) => lastPage.nextId,
48
+ * initialData: { pages: [], pageParams: [] },
49
+ * })
50
+ *
51
+ * return <>{data.pages.map((page) => page.projects.map((p) => <p key={p.id}>{p.name}</p>))}</>
52
+ * }
53
+ * ```
54
+ */
21
55
  export function useInfiniteQuery<
22
56
  TQueryFnData,
23
57
  TError = DefaultError,
@@ -35,6 +69,47 @@ export function useInfiniteQuery<
35
69
  queryClient?: QueryClient,
36
70
  ): DefinedUseInfiniteQueryResult<TData, TError>
37
71
 
72
+ /**
73
+ * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of `queryFn`,
74
+ * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
75
+ *
76
+ * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
77
+ * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
78
+ * actions, or add conditions like `hasNextPage && !isFetching`.
79
+ * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
80
+ * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
81
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
82
+ * be used.
83
+ * @returns The same properties as `useQuery`, with the addition of `data.pages`, `data.pageParams`,
84
+ * `fetchNextPage`, `fetchPreviousPage`, `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and
85
+ * `isFetchingPreviousPage`.
86
+ *
87
+ * @example
88
+ * ```tsx
89
+ * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
90
+ *
91
+ * const projectsOptions = infiniteQueryOptions({
92
+ * queryKey: ['projects'],
93
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
94
+ * initialPageParam: 0,
95
+ * getNextPageParam: (lastPage) => lastPage.nextId,
96
+ * })
97
+ *
98
+ * function Projects() {
99
+ * const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
100
+ * useInfiniteQuery(projectsOptions)
101
+ *
102
+ * return (
103
+ * <button
104
+ * onClick={() => fetchNextPage()}
105
+ * disabled={!hasNextPage || isFetchingNextPage}
106
+ * >
107
+ * Load More
108
+ * </button>
109
+ * )
110
+ * }
111
+ * ```
112
+ */
38
113
  export function useInfiniteQuery<
39
114
  TQueryFnData,
40
115
  TError = DefaultError,
@@ -52,6 +127,47 @@ export function useInfiniteQuery<
52
127
  queryClient?: QueryClient,
53
128
  ): UseInfiniteQueryResult<TData, TError>
54
129
 
130
+ /**
131
+ * The options for `useInfiniteQuery` are identical to `useQuery`, with the addition of `queryFn`,
132
+ * `initialPageParam`, `getNextPageParam`, `getPreviousPageParam`, and `maxPages`.
133
+ *
134
+ * @remarks Keep in mind that imperative fetch calls, such as `fetchNextPage`, may interfere with the default
135
+ * refetch behavior, resulting in outdated data. Make sure to call these functions only in response to user
136
+ * actions, or add conditions like `hasNextPage && !isFetching`.
137
+ * @see {@link infiniteQueryOptions} to share these options between `useInfiniteQuery` and imperative APIs like `queryClient.infiniteQuery`.
138
+ * @param options - The {@link UseInfiniteQueryOptions} to use — everything you can pass to `useInfiniteQuery`.
139
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
140
+ * be used.
141
+ * @returns The same properties as `useQuery`, with the addition of `data.pages`, `data.pageParams`,
142
+ * `fetchNextPage`, `fetchPreviousPage`, `hasNextPage`, `hasPreviousPage`, `isFetchingNextPage`, and
143
+ * `isFetchingPreviousPage`.
144
+ *
145
+ * @example
146
+ * ```tsx
147
+ * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/preact-query'
148
+ *
149
+ * const projectsOptions = infiniteQueryOptions({
150
+ * queryKey: ['projects'],
151
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
152
+ * initialPageParam: 0,
153
+ * getNextPageParam: (lastPage) => lastPage.nextId,
154
+ * })
155
+ *
156
+ * function Projects() {
157
+ * const { data, fetchNextPage, hasNextPage, isFetchingNextPage } =
158
+ * useInfiniteQuery(projectsOptions)
159
+ *
160
+ * return (
161
+ * <button
162
+ * onClick={() => fetchNextPage()}
163
+ * disabled={!hasNextPage || isFetchingNextPage}
164
+ * >
165
+ * Load More
166
+ * </button>
167
+ * )
168
+ * }
169
+ * ```
170
+ */
55
171
  export function useInfiniteQuery<
56
172
  TQueryFnData,
57
173
  TError = DefaultError,
@@ -5,6 +5,26 @@ import { useCallback } from 'preact/hooks'
5
5
  import { useQueryClient } from './QueryClientProvider'
6
6
  import { useSyncExternalStore } from './utils'
7
7
 
8
+ /**
9
+ * `useIsFetching` is an optional hook that returns the `number` of the queries that your application is loading or
10
+ * fetching in the background (useful for app-wide loading indicators).
11
+ *
12
+ * @param filters - The {@link QueryFilters} to narrow down the matched queries.
13
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
14
+ * be used.
15
+ * @returns Will be the `number` of the queries that your application is currently loading or fetching in the
16
+ * background.
17
+ *
18
+ * @example
19
+ * ```tsx
20
+ * import { useIsFetching } from '@tanstack/preact-query'
21
+ *
22
+ * // How many queries are fetching?
23
+ * const isFetching = useIsFetching()
24
+ * // How many queries matching the posts prefix are fetching?
25
+ * const isFetchingPosts = useIsFetching({ queryKey: ['posts'] })
26
+ * ```
27
+ */
8
28
  export function useIsFetching(
9
29
  filters?: QueryFilters,
10
30
  queryClient?: QueryClient,
@@ -17,6 +17,73 @@ import { useSyncExternalStore } from './utils'
17
17
 
18
18
  // HOOK
19
19
 
20
+ /**
21
+ * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.
22
+ * `useMutation` is the hook for that.
23
+ *
24
+ * @see {@link mutationOptions} to share these options across multiple `useMutation` call sites.
25
+ * @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.
26
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
27
+ * be used.
28
+ * @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second
29
+ * argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared
30
+ * mutation definition. If you make multiple requests, `onSuccess` will fire only after the latest call you've
31
+ * made.
32
+ *
33
+ * @example
34
+ * ```tsx
35
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
36
+ *
37
+ * function AddTodo() {
38
+ * const queryClient = useQueryClient()
39
+ *
40
+ * const addMutation = useMutation({
41
+ * mutationFn: addTodo,
42
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
43
+ * })
44
+ *
45
+ * return (
46
+ * <button onClick={() => addMutation.mutate('Item')}>Add</button>
47
+ * )
48
+ * }
49
+ * ```
50
+ *
51
+ * @example
52
+ * Optimistic update via `onMutate`, rolling back on `onError`:
53
+ * ```tsx
54
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
55
+ *
56
+ * function AddTodo() {
57
+ * const queryClient = useQueryClient()
58
+ *
59
+ * const addMutation = useMutation({
60
+ * mutationFn: addTodo,
61
+ * onMutate: async (newTodo) => {
62
+ * await queryClient.cancelQueries({ queryKey: ['todos'] })
63
+ * const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])
64
+ *
65
+ * queryClient.setQueryData<Array<string>>(['todos'], (old) => [
66
+ * ...(old ?? []),
67
+ * newTodo,
68
+ * ])
69
+ *
70
+ * // Passed to `onError` as `context` if the mutation fails.
71
+ * return { previousTodos }
72
+ * },
73
+ * onError: (_err, _newTodo, context) => {
74
+ * queryClient.setQueryData(['todos'], context?.previousTodos)
75
+ * },
76
+ * onSettled: () => {
77
+ * queryClient.invalidateQueries({ queryKey: ['todos'] })
78
+ * },
79
+ * })
80
+ *
81
+ * return (
82
+ * <button onClick={() => addMutation.mutate('Item')}>Add</button>
83
+ * )
84
+ * }
85
+ * ```
86
+ */
20
87
  export function useMutation<
21
88
  TData = unknown,
22
89
  TError = DefaultError,
@@ -11,6 +11,25 @@ import { useCallback, useEffect, useRef } from 'preact/hooks'
11
11
  import { useQueryClient } from './QueryClientProvider'
12
12
  import { useSyncExternalStore } from './utils'
13
13
 
14
+ /**
15
+ * `useIsMutating` is an optional hook that returns the `number` of mutations that your application is fetching
16
+ * (useful for app-wide loading indicators).
17
+ *
18
+ * @param filters - The {@link MutationFilters} to narrow down the matched mutations.
19
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
20
+ * be used.
21
+ * @returns Will be the `number` of the mutations that your application is currently fetching.
22
+ *
23
+ * @example
24
+ * ```tsx
25
+ * import { useIsMutating } from '@tanstack/preact-query'
26
+ *
27
+ * // How many mutations are fetching?
28
+ * const isMutating = useIsMutating()
29
+ * // How many mutations matching the posts prefix are fetching?
30
+ * const isMutatingPosts = useIsMutating({ mutationKey: ['posts'] })
31
+ * ```
32
+ */
14
33
  export function useIsMutating(
15
34
  filters?: MutationFilters,
16
35
  queryClient?: QueryClient,
@@ -60,6 +79,61 @@ function getResult<
60
79
  )
61
80
  }
62
81
 
82
+ /**
83
+ * `useMutationState` is a hook that gives you access to all mutations in the `MutationCache`. You can pass
84
+ * `filters` ({@link MutationFilters}) to narrow down your mutations, and `select` to transform the mutation
85
+ * state.
86
+ *
87
+ * @param options - The `filters` to narrow down matched mutations, and an optional `select` to transform the
88
+ * mutation state.
89
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
90
+ * be used.
91
+ * @returns Will be an Array of whatever `select` returns for each matching mutation.
92
+ *
93
+ * @example
94
+ * Get all variables of all running mutations:
95
+ * ```tsx
96
+ * import { useMutationState } from '@tanstack/preact-query'
97
+ *
98
+ * const variables = useMutationState({
99
+ * filters: { status: 'pending' },
100
+ * select: (mutation) => mutation.state.variables,
101
+ * })
102
+ * ```
103
+ *
104
+ * @example
105
+ * Get all data for specific mutations via the `mutationKey`:
106
+ * ```tsx
107
+ * import { useMutation, useMutationState } from '@tanstack/preact-query'
108
+ *
109
+ * const mutationKey = ['posts']
110
+ *
111
+ * // Some mutation that we want to get the state for
112
+ * const mutation = useMutation({
113
+ * mutationKey,
114
+ * mutationFn: createPosts,
115
+ * })
116
+ *
117
+ * const data = useMutationState({
118
+ * // this mutation key needs to match the mutation key of the given mutation (see above)
119
+ * filters: { mutationKey },
120
+ * select: (mutation) => mutation.state.data,
121
+ * })
122
+ * ```
123
+ *
124
+ * @example
125
+ * Access the latest mutation data via the `mutationKey`. Each invocation of `mutate` adds a new entry to the
126
+ * mutation cache for `gcTime` milliseconds — check the last item that `useMutationState` returns to get the
127
+ * latest invocation:
128
+ * ```tsx
129
+ * const data = useMutationState({
130
+ * filters: { mutationKey: ['posts'] },
131
+ * select: (mutation) => mutation.state.data,
132
+ * })
133
+ *
134
+ * const latest = data[data.length - 1]
135
+ * ```
136
+ */
63
137
  export function useMutationState<
64
138
  TResult = MutationState,
65
139
  TMutation extends Mutation<any, any, any, any> =
@@ -4,6 +4,50 @@ import { useQueryClient } from './QueryClientProvider'
4
4
  import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
5
5
  import type { UsePrefetchInfiniteQueryOptions } from './types'
6
6
 
7
+ /**
8
+ * `usePrefetchInfiniteQuery` does not return anything, it should be used just to fire a prefetch during render,
9
+ * before a suspense boundary that wraps a component that uses `useSuspenseInfiniteQuery`. You can pass
10
+ * everything to `usePrefetchInfiniteQuery` that you can pass to `queryClient.infiniteQuery`, though
11
+ * `queryKey`, `initialPageParam`, and `getNextPageParam` are always required, and `queryFn` is required unless
12
+ * a default query function has been defined.
13
+ *
14
+ * `getNextPageParam` receives both the last page of the infinite list of data and the full array of all pages,
15
+ * as well as pageParam information, and should return a single variable that will be passed to your query
16
+ * function as `context.pageParam`. Return `undefined` or `null` to indicate there is no next page available.
17
+ *
18
+ * The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
19
+ * over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
20
+ * already there or already in flight.
21
+ *
22
+ * @param options - The {@link UsePrefetchInfiniteQueryOptions} to use — everything you can pass to `queryClient.infiniteQuery`.
23
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
24
+ * be used.
25
+ * @returns `void` — nothing is returned.
26
+ *
27
+ * @example
28
+ * ```tsx
29
+ * import { Suspense } from 'preact/compat'
30
+ * import { infiniteQueryOptions, usePrefetchInfiniteQuery } from '@tanstack/preact-query'
31
+ *
32
+ * const projectsOptions = infiniteQueryOptions({
33
+ * queryKey: ['projects'],
34
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
35
+ * initialPageParam: 0,
36
+ * getNextPageParam: (lastPage) => lastPage.nextId,
37
+ * })
38
+ *
39
+ * function App() {
40
+ * // Fire the prefetch during render, before the suspense boundary below.
41
+ * usePrefetchInfiniteQuery(projectsOptions)
42
+ *
43
+ * return (
44
+ * <Suspense fallback={<h1>Loading projects...</h1>}>
45
+ * <Projects />
46
+ * </Suspense>
47
+ * )
48
+ * }
49
+ * ```
50
+ */
7
51
  export function usePrefetchInfiniteQuery<
8
52
  TQueryFnData = unknown,
9
53
  TError = DefaultError,
@@ -4,6 +4,41 @@ import { useQueryClient } from './QueryClientProvider'
4
4
  import type { DefaultError, QueryClient, QueryKey } from '@tanstack/query-core'
5
5
  import type { UsePrefetchQueryOptions } from './types'
6
6
 
7
+ /**
8
+ * `usePrefetchQuery` does not return anything, it should be used just to fire a prefetch during render, before
9
+ * a suspense boundary that wraps a component that uses `useSuspenseQuery`. You can pass everything to
10
+ * `usePrefetchQuery` that you can pass to `queryClient.query`, though `queryKey` is always required, and
11
+ * `queryFn` is required unless a default query function has been defined.
12
+ *
13
+ * The prefetch is skipped if the query already has any cached state — including a `pending`/`error` state left
14
+ * over from a previous attempt — so calling this on every render is cheap and won't refetch data that's
15
+ * already there or already in flight.
16
+ *
17
+ * @param options - The {@link UsePrefetchQueryOptions} to use — everything you can pass to `queryClient.query`.
18
+ * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will
19
+ * be used.
20
+ * @returns `void` — nothing is returned.
21
+ *
22
+ * @example
23
+ * ```tsx
24
+ * import { Suspense } from 'preact/compat'
25
+ * import { usePrefetchQuery } from '@tanstack/preact-query'
26
+ *
27
+ * function App() {
28
+ * // Fire the prefetch during render, before the suspense boundary below.
29
+ * usePrefetchQuery({
30
+ * queryKey: ['posts'],
31
+ * queryFn: fetchPosts,
32
+ * })
33
+ *
34
+ * return (
35
+ * <Suspense fallback={<h1>Loading posts...</h1>}>
36
+ * <Posts />
37
+ * </Suspense>
38
+ * )
39
+ * }
40
+ * ```
41
+ */
7
42
  export function usePrefetchQuery<
8
43
  TQueryFnData = unknown,
9
44
  TError = DefaultError,
@@ -23,6 +58,6 @@ export function usePrefetchQuery<
23
58
  const client = useQueryClient(queryClient)
24
59
 
25
60
  if (!client.getQueryState(options.queryKey)) {
26
- void client.fetchQuery(options).catch(noop)
61
+ void client.query(options).catch(noop)
27
62
  }
28
63
  }
package/src/useQueries.ts CHANGED
@@ -142,7 +142,16 @@ type GetUseQueryResult<T> =
142
142
  UseQueryResult
143
143
 
144
144
  /**
145
- * QueriesOptions reducer recursively unwraps function arguments to infer/enforce type param
145
+ * The `queries` array accepted by `useQueries`. Recursively unwraps each tuple element so every entry's
146
+ * `queryFn`/`select`/`throwOnError` are inferred individually, up to 20 elements. An opaque array (e.g.
147
+ * `unknown[]`) is returned as-is; a non-tuple array of a known element type, or a tuple past 20 elements, falls
148
+ * back to a single homogeneous options type.
149
+ *
150
+ * @template T - The type of the `queries` array as written at the call site.
151
+ * @template TResults - The internal accumulator that this type builds during recursion. It is not meant
152
+ * to be set explicitly.
153
+ * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
154
+ * meant to be set explicitly.
146
155
  */
147
156
  export type QueriesOptions<
148
157
  T extends Array<any>,
@@ -184,7 +193,16 @@ export type QueriesOptions<
184
193
  Array<UseQueryOptionsForUseQueries>
185
194
 
186
195
  /**
187
- * QueriesResults reducer recursively maps type param to results
196
+ * The result type returned by `useQueries`, when no `combine` is provided. Mirrors {@link QueriesOptions}: each
197
+ * tuple element's result type is inferred individually, up to 20 elements. A non-tuple array is mapped
198
+ * per-element instead, still inferring each entry individually; only past 20 elements does this fall back to a
199
+ * single homogeneous {@link UseQueryResult} type.
200
+ *
201
+ * @template T - The type of the `queries` array, as inferred by {@link QueriesOptions}.
202
+ * @template TResults - The internal accumulator that this type builds during recursion. It is not meant
203
+ * to be set explicitly.
204
+ * @template TDepth - The internal recursion-depth counter, checked against the 20-element limit. It is not
205
+ * meant to be set explicitly.
188
206
  */
189
207
  export type QueriesResults<
190
208
  T extends Array<any>,
@@ -204,6 +222,56 @@ export type QueriesResults<
204
222
  >
205
223
  : { [K in keyof T]: GetUseQueryResult<T[K]> }
206
224
 
225
+ /**
226
+ * The `useQueries` hook can be used to fetch a variable number of queries.
227
+ *
228
+ * The `queries` key accepts an array with query option objects identical to `useQuery` (excluding the
229
+ * `queryClient` option - because the `QueryClient` can be passed in on the top level).
230
+ *
231
+ * Having the same query key more than once in the array of query objects may cause some data to be shared
232
+ * between queries. To avoid this, consider de-duplicating the queries and map the results back to the desired
233
+ * structure.
234
+ *
235
+ * The `combine` option can be used to combine the results of the queries into a single value. The result will
236
+ * be structurally shared to be as referentially stable as possible.
237
+ *
238
+ * @param queryClient - Use this to provide a custom `QueryClient`. Otherwise, the one from the nearest context
239
+ * will be used.
240
+ * @returns The combined result. Without `combine`, this is an array with all the query results, in the same
241
+ * order as the input. When `combine` is provided, this is the value returned by `combine` instead.
242
+ *
243
+ * @example
244
+ * ```tsx
245
+ * import { useQueries } from '@tanstack/preact-query'
246
+ *
247
+ * const ids = [1, 2, 3]
248
+ * const results = useQueries({
249
+ * queries: ids.map((id) => ({
250
+ * queryKey: ['post', id],
251
+ * queryFn: () => fetchPost(id),
252
+ * staleTime: Infinity,
253
+ * })),
254
+ * })
255
+ * ```
256
+ *
257
+ * @example
258
+ * Combining results into a single value:
259
+ * ```tsx
260
+ * const ids = [1, 2, 3]
261
+ * const combinedQueries = useQueries({
262
+ * queries: ids.map((id) => ({
263
+ * queryKey: ['post', id],
264
+ * queryFn: () => fetchPost(id),
265
+ * })),
266
+ * combine: (results) => {
267
+ * return {
268
+ * data: results.map((result) => result.data),
269
+ * pending: results.some((result) => result.isPending),
270
+ * }
271
+ * },
272
+ * })
273
+ * ```
274
+ */
207
275
  export function useQueries<
208
276
  T extends Array<any>,
209
277
  TCombinedResult = QueriesResults<T>,
@@ -212,10 +280,25 @@ export function useQueries<
212
280
  queries,
213
281
  ...options
214
282
  }: {
283
+ /**
284
+ * An array with query option objects, mostly identical to `useQuery` — except that `queryClient` and
285
+ * `subscribed` aren't accepted per-query (`subscribed` is a top-level option here instead), and
286
+ * `placeholderData` accepts a {@link QueriesPlaceholderDataFunction}, which is called with `previousData`
287
+ * and `previousQuery` always `undefined`, rather than `useQuery`'s placeholder function.
288
+ */
215
289
  queries:
216
290
  | readonly [...QueriesOptions<T>]
217
291
  | readonly [...{ [K in keyof T]: GetUseQueryOptionsForUseQueries<T[K]> }]
292
+ /**
293
+ * Use this to combine the results of the queries into a single value. The result will be structurally
294
+ * shared to be as referentially stable as possible.
295
+ */
218
296
  combine?: (result: QueriesResults<T>) => TCombinedResult
297
+ /**
298
+ * Set this to `false` to unsubscribe this observer from updates to the query cache.
299
+ *
300
+ * @defaultValue true
301
+ */
219
302
  subscribed?: boolean
220
303
  },
221
304
  queryClient?: QueryClient,