@tanstack/react-query 5.102.8 → 5.103.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (298) hide show
  1. package/build/legacy/HydrationBoundary.cjs +44 -1
  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 +44 -1
  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 +76 -0
  26. package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
  27. package/build/legacy/QueryErrorResetBoundary.d.cts +77 -0
  28. package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
  29. package/build/legacy/QueryErrorResetBoundary.d.ts +77 -0
  30. package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
  31. package/build/legacy/QueryErrorResetBoundary.js +76 -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 +173 -0
  35. package/build/legacy/infiniteQueryOptions.d.cts.map +1 -1
  36. package/build/legacy/infiniteQueryOptions.d.ts +173 -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 +55 -0
  41. package/build/legacy/mutationOptions.d.cts.map +1 -1
  42. package/build/legacy/mutationOptions.d.ts +55 -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 +175 -0
  47. package/build/legacy/queryOptions.d.cts.map +1 -1
  48. package/build/legacy/queryOptions.d.ts +175 -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 +292 -0
  59. package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
  60. package/build/legacy/useInfiniteQuery.d.ts +292 -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 +36 -0
  64. package/build/legacy/useIsFetching.cjs.map +1 -1
  65. package/build/legacy/useIsFetching.d.cts +36 -0
  66. package/build/legacy/useIsFetching.d.cts.map +1 -1
  67. package/build/legacy/useIsFetching.d.ts +36 -0
  68. package/build/legacy/useIsFetching.d.ts.map +1 -1
  69. package/build/legacy/useIsFetching.js +36 -0
  70. package/build/legacy/useIsFetching.js.map +1 -1
  71. package/build/legacy/useMutation.cjs +172 -0
  72. package/build/legacy/useMutation.cjs.map +1 -1
  73. package/build/legacy/useMutation.d.cts +172 -0
  74. package/build/legacy/useMutation.d.cts.map +1 -1
  75. package/build/legacy/useMutation.d.ts +172 -0
  76. package/build/legacy/useMutation.d.ts.map +1 -1
  77. package/build/legacy/useMutation.js +172 -0
  78. package/build/legacy/useMutation.js.map +1 -1
  79. package/build/legacy/useMutationState.cjs +94 -0
  80. package/build/legacy/useMutationState.cjs.map +1 -1
  81. package/build/legacy/useMutationState.d.cts +94 -0
  82. package/build/legacy/useMutationState.d.cts.map +1 -1
  83. package/build/legacy/useMutationState.d.ts +94 -0
  84. package/build/legacy/useMutationState.d.ts.map +1 -1
  85. package/build/legacy/useMutationState.js +94 -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 +35 -0
  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 +35 -0
  102. package/build/legacy/usePrefetchQuery.js.map +1 -1
  103. package/build/legacy/useQueries.cjs +130 -0
  104. package/build/legacy/useQueries.cjs.map +1 -1
  105. package/build/legacy/useQueries.d.cts +165 -2
  106. package/build/legacy/useQueries.d.cts.map +1 -1
  107. package/build/legacy/useQueries.d.ts +165 -2
  108. package/build/legacy/useQueries.d.ts.map +1 -1
  109. package/build/legacy/useQueries.js +130 -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 +246 -0
  113. package/build/legacy/useQuery.d.cts.map +1 -1
  114. package/build/legacy/useQuery.d.ts +246 -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 +86 -0
  118. package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
  119. package/build/legacy/useSuspenseInfiniteQuery.d.cts +86 -0
  120. package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
  121. package/build/legacy/useSuspenseInfiniteQuery.d.ts +86 -0
  122. package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
  123. package/build/legacy/useSuspenseInfiniteQuery.js +86 -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 +320 -2
  127. package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
  128. package/build/legacy/useSuspenseQueries.d.ts +320 -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 +67 -0
  132. package/build/legacy/useSuspenseQuery.cjs.map +1 -1
  133. package/build/legacy/useSuspenseQuery.d.cts +67 -0
  134. package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
  135. package/build/legacy/useSuspenseQuery.d.ts +67 -0
  136. package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
  137. package/build/legacy/useSuspenseQuery.js +67 -0
  138. package/build/legacy/useSuspenseQuery.js.map +1 -1
  139. package/build/modern/HydrationBoundary.cjs +44 -1
  140. package/build/modern/HydrationBoundary.cjs.map +1 -1
  141. package/build/modern/HydrationBoundary.d.cts +61 -0
  142. package/build/modern/HydrationBoundary.d.cts.map +1 -1
  143. package/build/modern/HydrationBoundary.d.ts +61 -0
  144. package/build/modern/HydrationBoundary.d.ts.map +1 -1
  145. package/build/modern/HydrationBoundary.js +44 -1
  146. package/build/modern/HydrationBoundary.js.map +1 -1
  147. package/build/modern/IsRestoringProvider.cjs +11 -0
  148. package/build/modern/IsRestoringProvider.cjs.map +1 -1
  149. package/build/modern/IsRestoringProvider.d.cts +11 -0
  150. package/build/modern/IsRestoringProvider.d.cts.map +1 -1
  151. package/build/modern/IsRestoringProvider.d.ts +11 -0
  152. package/build/modern/IsRestoringProvider.d.ts.map +1 -1
  153. package/build/modern/IsRestoringProvider.js +11 -0
  154. package/build/modern/IsRestoringProvider.js.map +1 -1
  155. package/build/modern/QueryClientProvider.cjs +30 -0
  156. package/build/modern/QueryClientProvider.cjs.map +1 -1
  157. package/build/modern/QueryClientProvider.d.cts +41 -0
  158. package/build/modern/QueryClientProvider.d.cts.map +1 -1
  159. package/build/modern/QueryClientProvider.d.ts +41 -0
  160. package/build/modern/QueryClientProvider.d.ts.map +1 -1
  161. package/build/modern/QueryClientProvider.js +30 -0
  162. package/build/modern/QueryClientProvider.js.map +1 -1
  163. package/build/modern/QueryErrorResetBoundary.cjs +76 -0
  164. package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
  165. package/build/modern/QueryErrorResetBoundary.d.cts +77 -0
  166. package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
  167. package/build/modern/QueryErrorResetBoundary.d.ts +77 -0
  168. package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
  169. package/build/modern/QueryErrorResetBoundary.js +76 -0
  170. package/build/modern/QueryErrorResetBoundary.js.map +1 -1
  171. package/build/modern/infiniteQueryOptions.cjs.map +1 -1
  172. package/build/modern/infiniteQueryOptions.d.cts +173 -0
  173. package/build/modern/infiniteQueryOptions.d.cts.map +1 -1
  174. package/build/modern/infiniteQueryOptions.d.ts +173 -0
  175. package/build/modern/infiniteQueryOptions.d.ts.map +1 -1
  176. package/build/modern/infiniteQueryOptions.js.map +1 -1
  177. package/build/modern/mutationOptions.cjs.map +1 -1
  178. package/build/modern/mutationOptions.d.cts +55 -0
  179. package/build/modern/mutationOptions.d.cts.map +1 -1
  180. package/build/modern/mutationOptions.d.ts +55 -0
  181. package/build/modern/mutationOptions.d.ts.map +1 -1
  182. package/build/modern/mutationOptions.js.map +1 -1
  183. package/build/modern/queryOptions.cjs.map +1 -1
  184. package/build/modern/queryOptions.d.cts +175 -0
  185. package/build/modern/queryOptions.d.cts.map +1 -1
  186. package/build/modern/queryOptions.d.ts +175 -0
  187. package/build/modern/queryOptions.d.ts.map +1 -1
  188. package/build/modern/queryOptions.js.map +1 -1
  189. package/build/modern/types.cjs.map +1 -1
  190. package/build/modern/types.d.cts +228 -2
  191. package/build/modern/types.d.cts.map +1 -1
  192. package/build/modern/types.d.ts +228 -2
  193. package/build/modern/types.d.ts.map +1 -1
  194. package/build/modern/types.js.map +1 -1
  195. package/build/modern/useInfiniteQuery.cjs.map +1 -1
  196. package/build/modern/useInfiniteQuery.d.cts +292 -0
  197. package/build/modern/useInfiniteQuery.d.cts.map +1 -1
  198. package/build/modern/useInfiniteQuery.d.ts +292 -0
  199. package/build/modern/useInfiniteQuery.d.ts.map +1 -1
  200. package/build/modern/useInfiniteQuery.js.map +1 -1
  201. package/build/modern/useIsFetching.cjs +36 -0
  202. package/build/modern/useIsFetching.cjs.map +1 -1
  203. package/build/modern/useIsFetching.d.cts +36 -0
  204. package/build/modern/useIsFetching.d.cts.map +1 -1
  205. package/build/modern/useIsFetching.d.ts +36 -0
  206. package/build/modern/useIsFetching.d.ts.map +1 -1
  207. package/build/modern/useIsFetching.js +36 -0
  208. package/build/modern/useIsFetching.js.map +1 -1
  209. package/build/modern/useMutation.cjs +172 -0
  210. package/build/modern/useMutation.cjs.map +1 -1
  211. package/build/modern/useMutation.d.cts +172 -0
  212. package/build/modern/useMutation.d.cts.map +1 -1
  213. package/build/modern/useMutation.d.ts +172 -0
  214. package/build/modern/useMutation.d.ts.map +1 -1
  215. package/build/modern/useMutation.js +172 -0
  216. package/build/modern/useMutation.js.map +1 -1
  217. package/build/modern/useMutationState.cjs +94 -0
  218. package/build/modern/useMutationState.cjs.map +1 -1
  219. package/build/modern/useMutationState.d.cts +94 -0
  220. package/build/modern/useMutationState.d.cts.map +1 -1
  221. package/build/modern/useMutationState.d.ts +94 -0
  222. package/build/modern/useMutationState.d.ts.map +1 -1
  223. package/build/modern/useMutationState.js +94 -0
  224. package/build/modern/useMutationState.js.map +1 -1
  225. package/build/modern/usePrefetchInfiniteQuery.cjs +44 -0
  226. package/build/modern/usePrefetchInfiniteQuery.cjs.map +1 -1
  227. package/build/modern/usePrefetchInfiniteQuery.d.cts +44 -0
  228. package/build/modern/usePrefetchInfiniteQuery.d.cts.map +1 -1
  229. package/build/modern/usePrefetchInfiniteQuery.d.ts +44 -0
  230. package/build/modern/usePrefetchInfiniteQuery.d.ts.map +1 -1
  231. package/build/modern/usePrefetchInfiniteQuery.js +44 -0
  232. package/build/modern/usePrefetchInfiniteQuery.js.map +1 -1
  233. package/build/modern/usePrefetchQuery.cjs +35 -0
  234. package/build/modern/usePrefetchQuery.cjs.map +1 -1
  235. package/build/modern/usePrefetchQuery.d.cts +35 -0
  236. package/build/modern/usePrefetchQuery.d.cts.map +1 -1
  237. package/build/modern/usePrefetchQuery.d.ts +35 -0
  238. package/build/modern/usePrefetchQuery.d.ts.map +1 -1
  239. package/build/modern/usePrefetchQuery.js +35 -0
  240. package/build/modern/usePrefetchQuery.js.map +1 -1
  241. package/build/modern/useQueries.cjs +130 -0
  242. package/build/modern/useQueries.cjs.map +1 -1
  243. package/build/modern/useQueries.d.cts +165 -2
  244. package/build/modern/useQueries.d.cts.map +1 -1
  245. package/build/modern/useQueries.d.ts +165 -2
  246. package/build/modern/useQueries.d.ts.map +1 -1
  247. package/build/modern/useQueries.js +130 -0
  248. package/build/modern/useQueries.js.map +1 -1
  249. package/build/modern/useQuery.cjs.map +1 -1
  250. package/build/modern/useQuery.d.cts +246 -0
  251. package/build/modern/useQuery.d.cts.map +1 -1
  252. package/build/modern/useQuery.d.ts +246 -0
  253. package/build/modern/useQuery.d.ts.map +1 -1
  254. package/build/modern/useQuery.js.map +1 -1
  255. package/build/modern/useSuspenseInfiniteQuery.cjs +86 -0
  256. package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
  257. package/build/modern/useSuspenseInfiniteQuery.d.cts +86 -0
  258. package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
  259. package/build/modern/useSuspenseInfiniteQuery.d.ts +86 -0
  260. package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
  261. package/build/modern/useSuspenseInfiniteQuery.js +86 -0
  262. package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
  263. package/build/modern/useSuspenseQueries.cjs.map +1 -1
  264. package/build/modern/useSuspenseQueries.d.cts +320 -2
  265. package/build/modern/useSuspenseQueries.d.cts.map +1 -1
  266. package/build/modern/useSuspenseQueries.d.ts +320 -2
  267. package/build/modern/useSuspenseQueries.d.ts.map +1 -1
  268. package/build/modern/useSuspenseQueries.js.map +1 -1
  269. package/build/modern/useSuspenseQuery.cjs +67 -0
  270. package/build/modern/useSuspenseQuery.cjs.map +1 -1
  271. package/build/modern/useSuspenseQuery.d.cts +67 -0
  272. package/build/modern/useSuspenseQuery.d.cts.map +1 -1
  273. package/build/modern/useSuspenseQuery.d.ts +67 -0
  274. package/build/modern/useSuspenseQuery.d.ts.map +1 -1
  275. package/build/modern/useSuspenseQuery.js +67 -0
  276. package/build/modern/useSuspenseQuery.js.map +1 -1
  277. package/build/query-codemods/eslint.config.js +7 -0
  278. package/build/query-codemods/root.eslint.config.js +45 -0
  279. package/package.json +11 -11
  280. package/src/HydrationBoundary.tsx +61 -1
  281. package/src/IsRestoringProvider.ts +12 -0
  282. package/src/QueryClientProvider.tsx +41 -0
  283. package/src/QueryErrorResetBoundary.tsx +89 -0
  284. package/src/infiniteQueryOptions.ts +173 -0
  285. package/src/mutationOptions.ts +55 -0
  286. package/src/queryOptions.ts +175 -0
  287. package/src/types.ts +228 -2
  288. package/src/useInfiniteQuery.ts +292 -0
  289. package/src/useIsFetching.ts +36 -0
  290. package/src/useMutation.ts +172 -0
  291. package/src/useMutationState.ts +94 -0
  292. package/src/usePrefetchInfiniteQuery.tsx +44 -0
  293. package/src/usePrefetchQuery.tsx +35 -0
  294. package/src/useQueries.ts +165 -2
  295. package/src/useQuery.ts +246 -0
  296. package/src/useSuspenseInfiniteQuery.ts +86 -0
  297. package/src/useSuspenseQueries.ts +320 -2
  298. package/src/useSuspenseQuery.ts +67 -0
@@ -10,6 +10,17 @@ import type {
10
10
  } from '@tanstack/query-core'
11
11
  import type { UseInfiniteQueryOptions } from './types'
12
12
 
13
+ /**
14
+ * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set — `data`
15
+ * may be `undefined` while the query is `pending`.
16
+ *
17
+ * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
18
+ * @template TError - The type of errors your `queryFn` may throw.
19
+ * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
20
+ * the shape of all fetched pages plus their page params.
21
+ * @template TQueryKey - The type of your `queryKey`.
22
+ * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
23
+ */
13
24
  export type UndefinedInitialDataInfiniteOptions<
14
25
  TQueryFnData,
15
26
  TError = DefaultError,
@@ -23,6 +34,13 @@ export type UndefinedInitialDataInfiniteOptions<
23
34
  TQueryKey,
24
35
  TPageParam
25
36
  > & {
37
+ /**
38
+ * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
39
+ * created or cached yet). If set to a function, the function will be called **once** during the shared/root
40
+ * query initialization, and be expected to synchronously return the initial data. Initial data is
41
+ * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
42
+ * cache.
43
+ */
26
44
  initialData?:
27
45
  | undefined
28
46
  | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
@@ -31,6 +49,18 @@ export type UndefinedInitialDataInfiniteOptions<
31
49
  >
32
50
  }
33
51
 
52
+ /**
53
+ * The options accepted by the `infiniteQueryOptions` overload selected when no `initialData` is set and
54
+ * `queryFn` is not `skipToken` — same as {@link UndefinedInitialDataInfiniteOptions}, but `queryFn` may not be
55
+ * `skipToken`.
56
+ *
57
+ * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
58
+ * @template TError - The type of errors your `queryFn` may throw.
59
+ * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
60
+ * the shape of all fetched pages plus their page params.
61
+ * @template TQueryKey - The type of your `queryKey`.
62
+ * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
63
+ */
34
64
  export type UnusedSkipTokenInfiniteOptions<
35
65
  TQueryFnData,
36
66
  TError = DefaultError,
@@ -41,6 +71,12 @@ export type UnusedSkipTokenInfiniteOptions<
41
71
  UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryKey, TPageParam>,
42
72
  'queryFn'
43
73
  > & {
74
+ /**
75
+ * `skipToken` is not allowed as a value here — this overload is selected when no `initialData` is set. If
76
+ * you don't intend to run the query yet, set `enabled: false` — omitting `queryFn` alone still triggers a
77
+ * fetch that fails with "Missing queryFn" unless `enabled` is `false` or a default query function has been
78
+ * defined. A default query function only supplies `queryFn`; it doesn't defer the fetch on its own.
79
+ */
44
80
  queryFn?: Exclude<
45
81
  UseInfiniteQueryOptions<
46
82
  TQueryFnData,
@@ -53,6 +89,17 @@ export type UnusedSkipTokenInfiniteOptions<
53
89
  >
54
90
  }
55
91
 
92
+ /**
93
+ * The options accepted by the `infiniteQueryOptions` overload selected when `initialData` is set — `data` is
94
+ * never `undefined`.
95
+ *
96
+ * @template TQueryFnData - The type of a single page, as your `queryFn` resolves it.
97
+ * @template TError - The type of errors your `queryFn` may throw.
98
+ * @template TData - The type `data` ends up as after `select` runs — defaults to `InfiniteData<TQueryFnData>`,
99
+ * the shape of all fetched pages plus their page params.
100
+ * @template TQueryKey - The type of your `queryKey`.
101
+ * @template TPageParam - The type of the parameter passed to `queryFn` to fetch a given page.
102
+ */
56
103
  export type DefinedInitialDataInfiniteOptions<
57
104
  TQueryFnData,
58
105
  TError = DefaultError,
@@ -66,12 +113,60 @@ export type DefinedInitialDataInfiniteOptions<
66
113
  TQueryKey,
67
114
  TPageParam
68
115
  > & {
116
+ /**
117
+ * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
118
+ * created or cached yet). If set to a function, the function will be called **once** during the shared/root
119
+ * query initialization, and be expected to synchronously return the initial data. Initial data is
120
+ * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
121
+ * cache.
122
+ */
69
123
  initialData:
70
124
  | NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>
71
125
  | (() => NonUndefinedGuard<InfiniteData<TQueryFnData, TPageParam>>)
72
126
  | undefined
73
127
  }
74
128
 
129
+ /**
130
+ * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
131
+ * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
132
+ * `options.queryKey` is required and is the query key to generate options for.
133
+ *
134
+ * This overload is selected when `initialData` is set.
135
+ *
136
+ * @see {@link useInfiniteQuery} to run an infinite query with these options.
137
+ * @param options - The {@link DefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`, with `initialData` set.
138
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
139
+ * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
140
+ * automatically as the user scrolls.
141
+ *
142
+ * @example
143
+ * ```tsx
144
+ * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/react-query'
145
+ *
146
+ * export const projectsOptions = infiniteQueryOptions({
147
+ * queryKey: ['projects'],
148
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
149
+ * initialPageParam: 0,
150
+ * getNextPageParam: (lastPage) => lastPage.nextId,
151
+ * initialData: { pages: [], pageParams: [] },
152
+ * })
153
+ *
154
+ * function Projects() {
155
+ * // `data` is never `undefined`, thanks to `initialData` — even if a refetch fails, so the
156
+ * // list stays visible alongside the error.
157
+ * const { data, isError, error } = useInfiniteQuery(projectsOptions)
158
+ *
159
+ * return (
160
+ * <div>
161
+ * {isError ? <span>Error: {error.message}</span> : null}
162
+ * <ul>
163
+ * {data.pages.map((page) => page.projects.map((p) => <li key={p.id}>{p.name}</li>))}
164
+ * </ul>
165
+ * </div>
166
+ * )
167
+ * }
168
+ * ```
169
+ */
75
170
  export function infiniteQueryOptions<
76
171
  TQueryFnData,
77
172
  TError = DefaultError,
@@ -95,6 +190,45 @@ export function infiniteQueryOptions<
95
190
  > &
96
191
  QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
97
192
 
193
+ /**
194
+ * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
195
+ * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
196
+ * `options.queryKey` is required and is the query key to generate options for.
197
+ *
198
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
199
+ * @remarks See {@link useInfiniteQuery} for examples that fetch further pages, from a button click or
200
+ * automatically as the user scrolls.
201
+ *
202
+ * @example
203
+ * A parameterized factory, so the same options object can be reused per `postId`:
204
+ * ```tsx
205
+ * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/react-query'
206
+ *
207
+ * export const commentsOptions = (postId: string) =>
208
+ * infiniteQueryOptions({
209
+ * queryKey: ['post', postId, 'comments'],
210
+ * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
211
+ * initialPageParam: 0,
212
+ * getNextPageParam: (lastPage) => lastPage.nextId,
213
+ * })
214
+ *
215
+ * function Comments({ postId }: { postId: string }) {
216
+ * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
217
+ *
218
+ * if (isPending) return 'Loading...'
219
+ * if (isError) return <span>Error: {error.message}</span>
220
+ *
221
+ * return (
222
+ * <ul>
223
+ * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
224
+ * </ul>
225
+ * )
226
+ * }
227
+ * ```
228
+ *
229
+ * @see {@link useInfiniteQuery} to run an infinite query with these options.
230
+ * @param options - The {@link UnusedSkipTokenInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
231
+ */
98
232
  export function infiniteQueryOptions<
99
233
  TQueryFnData,
100
234
  TError = DefaultError,
@@ -118,6 +252,45 @@ export function infiniteQueryOptions<
118
252
  > &
119
253
  QueryKeyWithDataTag<TQueryKey, InfiniteData<TQueryFnData>, TError>
120
254
 
255
+ /**
256
+ * You can generally pass everything to `infiniteQueryOptions` that you can also pass to `useInfiniteQuery`.
257
+ * These options can be shared across hooks and imperative APIs such as `queryClient.infiniteQuery`.
258
+ * `options.queryKey` is required and is the query key to generate options for.
259
+ *
260
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
261
+ * @remarks See {@link useInfiniteQuery} for examples that fetch further pages (from a button click or
262
+ * automatically as the user scrolls) and that use `skipToken` to disable the query until `postId` is set.
263
+ *
264
+ * @example
265
+ * A parameterized factory, so the same options object can be reused per `postId`:
266
+ * ```tsx
267
+ * import { infiniteQueryOptions, useInfiniteQuery } from '@tanstack/react-query'
268
+ *
269
+ * export const commentsOptions = (postId: string) =>
270
+ * infiniteQueryOptions({
271
+ * queryKey: ['post', postId, 'comments'],
272
+ * queryFn: ({ pageParam }) => fetchComments(postId, pageParam),
273
+ * initialPageParam: 0,
274
+ * getNextPageParam: (lastPage) => lastPage.nextId,
275
+ * })
276
+ *
277
+ * function Comments({ postId }: { postId: string }) {
278
+ * const { data, isPending, isError, error } = useInfiniteQuery(commentsOptions(postId))
279
+ *
280
+ * if (isPending) return 'Loading...'
281
+ * if (isError) return <span>Error: {error.message}</span>
282
+ *
283
+ * return (
284
+ * <ul>
285
+ * {data.pages.map((page) => page.comments.map((c) => <li key={c.id}>{c.text}</li>))}
286
+ * </ul>
287
+ * )
288
+ * }
289
+ * ```
290
+ *
291
+ * @see {@link useInfiniteQuery} to run an infinite query with these options.
292
+ * @param options - The {@link UndefinedInitialDataInfiniteOptions} to use — everything you can pass to `useInfiniteQuery`.
293
+ */
121
294
  export function infiniteQueryOptions<
122
295
  TQueryFnData,
123
296
  TError = DefaultError,
@@ -1,6 +1,35 @@
1
1
  import type { DefaultError, WithRequired } from '@tanstack/query-core'
2
2
  import type { UseMutationOptions } from './types'
3
3
 
4
+ /**
5
+ * You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. A
6
+ * `mutationKey` is required on this overload so the mutation can be looked up later, e.g. with
7
+ * `useMutationState`.
8
+ *
9
+ * @see {@link useMutation} to run the mutation these options describe.
10
+ * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, with a
11
+ * required `mutationKey`.
12
+ * @returns The same options object, unchanged.
13
+ *
14
+ * @example
15
+ * Looking the mutation up elsewhere via its `mutationKey`, e.g. for a global "saving…" indicator:
16
+ * ```tsx
17
+ * import { mutationOptions, useMutationState } from '@tanstack/react-query'
18
+ *
19
+ * const createPostOptions = mutationOptions({
20
+ * mutationKey: ['posts', 'create'],
21
+ * mutationFn: createPost,
22
+ * })
23
+ *
24
+ * function SavingIndicator() {
25
+ * const isCreatingPost = useMutationState({
26
+ * filters: { mutationKey: createPostOptions.mutationKey, status: 'pending' },
27
+ * }).length > 0
28
+ *
29
+ * return isCreatingPost ? <span>Saving…</span> : null
30
+ * }
31
+ * ```
32
+ */
4
33
  export function mutationOptions<
5
34
  TData = unknown,
6
35
  TError = DefaultError,
@@ -15,6 +44,32 @@ export function mutationOptions<
15
44
  UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,
16
45
  'mutationKey'
17
46
  >
47
+ /**
48
+ * You can generally pass everything to `mutationOptions` that you can also pass to `useMutation`. No
49
+ * `mutationKey` is required on this overload — use this when you don't need to target the mutation via a
50
+ * `mutationKey` filter later (e.g. with `useMutationState`); it can still be observed through other filters,
51
+ * such as `status`.
52
+ *
53
+ * @see {@link useMutation} to run the mutation these options describe.
54
+ * @param options - The mutation options to use, identical to what you'd pass to `useMutation`, without a
55
+ * `mutationKey`.
56
+ * @returns The same options object, unchanged.
57
+ * @remarks See the other overload's example for looking a mutation up via `useMutationState`.
58
+ *
59
+ * @example
60
+ * ```tsx
61
+ * import { mutationOptions, useMutation } from '@tanstack/react-query'
62
+ *
63
+ * export const createPostOptions = mutationOptions({
64
+ * mutationFn: createPost,
65
+ * })
66
+ *
67
+ * function CreatePost() {
68
+ * const mutation = useMutation(createPostOptions)
69
+ * return <button onClick={() => mutation.mutate({ title: 'Hello' })}>Create</button>
70
+ * }
71
+ * ```
72
+ */
18
73
  export function mutationOptions<
19
74
  TData = unknown,
20
75
  TError = DefaultError,
@@ -10,18 +10,43 @@ import type {
10
10
  } from '@tanstack/query-core'
11
11
  import type { UseQueryOptions } from './types'
12
12
 
13
+ /**
14
+ * The options accepted by the `queryOptions` overload selected when no `initialData` is set — `data` may be
15
+ * `undefined` while the query is `pending`.
16
+ *
17
+ * @template TQueryFnData - The type your `queryFn` resolves to.
18
+ * @template TError - The type of errors your `queryFn` may throw.
19
+ * @template TData - The type `data` ends up as after `select` runs.
20
+ * @template TQueryKey - The type of your `queryKey`.
21
+ */
13
22
  export type UndefinedInitialDataOptions<
14
23
  TQueryFnData = unknown,
15
24
  TError = DefaultError,
16
25
  TData = TQueryFnData,
17
26
  TQueryKey extends QueryKey = QueryKey,
18
27
  > = UseQueryOptions<TQueryFnData, TError, TData, TQueryKey> & {
28
+ /**
29
+ * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
30
+ * created or cached yet). If set to a function, the function will be called **once** during the shared/root
31
+ * query initialization, and be expected to synchronously return the initial data. Initial data is
32
+ * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
33
+ * cache.
34
+ */
19
35
  initialData?:
20
36
  | undefined
21
37
  | InitialDataFunction<NonUndefinedGuard<TQueryFnData>>
22
38
  | NonUndefinedGuard<TQueryFnData>
23
39
  }
24
40
 
41
+ /**
42
+ * The options accepted by the `queryOptions` overload selected when no `initialData` is set and `queryFn` is
43
+ * not `skipToken` — same as {@link UndefinedInitialDataOptions}, but `queryFn` may not be `skipToken`.
44
+ *
45
+ * @template TQueryFnData - The type your `queryFn` resolves to.
46
+ * @template TError - The type of errors your `queryFn` may throw.
47
+ * @template TData - The type `data` ends up as after `select` runs.
48
+ * @template TQueryKey - The type of your `queryKey`.
49
+ */
25
50
  export type UnusedSkipTokenOptions<
26
51
  TQueryFnData = unknown,
27
52
  TError = DefaultError,
@@ -31,24 +56,89 @@ export type UnusedSkipTokenOptions<
31
56
  UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>,
32
57
  'queryFn'
33
58
  > & {
59
+ /**
60
+ * `skipToken` is not allowed as a value here — this overload is selected when no `initialData` is set. If
61
+ * you don't intend to run the query yet, set `enabled: false` — omitting `queryFn` alone still triggers a
62
+ * fetch that fails with "Missing queryFn" unless `enabled` is `false` or a default query function has been
63
+ * defined. A default query function only supplies `queryFn`; it doesn't defer the fetch on its own.
64
+ */
34
65
  queryFn?: Exclude<
35
66
  UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>['queryFn'],
36
67
  SkipToken | undefined
37
68
  >
38
69
  }
39
70
 
71
+ /**
72
+ * The options accepted by the `queryOptions` overload selected when `initialData` is set — `data` is never
73
+ * `undefined`.
74
+ *
75
+ * @template TQueryFnData - The type your `queryFn` resolves to.
76
+ * @template TError - The type of errors your `queryFn` may throw.
77
+ * @template TData - The type `data` ends up as after `select` runs.
78
+ * @template TQueryKey - The type of your `queryKey`.
79
+ */
40
80
  export type DefinedInitialDataOptions<
41
81
  TQueryFnData = unknown,
42
82
  TError = DefaultError,
43
83
  TData = TQueryFnData,
44
84
  TQueryKey extends QueryKey = QueryKey,
45
85
  > = Omit<UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, 'queryFn'> & {
86
+ /**
87
+ * If set, this value will be used as the initial data for the query cache (as long as the query hasn't been
88
+ * created or cached yet). If set to a function, the function will be called **once** during the shared/root
89
+ * query initialization, and be expected to synchronously return the initial data. Initial data is
90
+ * considered stale by default unless a `staleTime` has been set. `initialData` **is persisted** to the
91
+ * cache.
92
+ */
46
93
  initialData:
47
94
  | NonUndefinedGuard<TQueryFnData>
48
95
  | (() => NonUndefinedGuard<TQueryFnData>)
96
+ /**
97
+ * Optional here, but omitting it is only safe when no fetch will be attempted — for example with
98
+ * `enabled: false`, or when a default query function has been defined. Otherwise, an enabled query with no
99
+ * `queryFn` still tries to fetch and fails with a "Missing queryFn" error; `initialData` does not prevent this.
100
+ */
49
101
  queryFn?: QueryFunction<TQueryFnData, TQueryKey>
50
102
  }
51
103
 
104
+ /**
105
+ * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
106
+ * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
107
+ * is the query key to generate options for.
108
+ *
109
+ * This overload is selected when `initialData` is set, so the resulting `data` is never `undefined`.
110
+ *
111
+ * @see {@link useQuery} to run a query with these options.
112
+ * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
113
+ * @param options - The {@link DefinedInitialDataOptions} to use — everything you can pass to `useQuery`, with `initialData` set.
114
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
115
+ *
116
+ * @example
117
+ * ```tsx
118
+ * import { queryOptions, useQuery } from '@tanstack/react-query'
119
+ *
120
+ * export const postsOptions = queryOptions({
121
+ * queryKey: ['posts'],
122
+ * queryFn: fetchPosts,
123
+ * initialData: [],
124
+ * })
125
+ *
126
+ * function Posts() {
127
+ * // `data` is `Post[]`, never `undefined`, thanks to `initialData` — even if a refetch fails,
128
+ * // so the list stays visible alongside the error.
129
+ * const { data, isError, error } = useQuery(postsOptions)
130
+ *
131
+ * return (
132
+ * <div>
133
+ * {isError ? <span>Error: {error.message}</span> : null}
134
+ * <ul>
135
+ * {data.map((post) => <li key={post.id}>{post.title}</li>)}
136
+ * </ul>
137
+ * </div>
138
+ * )
139
+ * }
140
+ * ```
141
+ */
52
142
  export function queryOptions<
53
143
  TQueryFnData = unknown,
54
144
  TError = DefaultError,
@@ -59,6 +149,37 @@ export function queryOptions<
59
149
  ): DefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey> &
60
150
  QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
61
151
 
152
+ /**
153
+ * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
154
+ * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
155
+ * is the query key to generate options for.
156
+ *
157
+ * @see {@link useQuery} to run a query with these options.
158
+ * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
159
+ * @param options - The {@link UnusedSkipTokenOptions} to use — everything you can pass to `useQuery`.
160
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
161
+ *
162
+ * @example
163
+ * A parameterized factory, so the same options object can be reused per `id`:
164
+ * ```tsx
165
+ * import { queryOptions, useQuery } from '@tanstack/react-query'
166
+ *
167
+ * export const postOptions = (id: string) =>
168
+ * queryOptions({
169
+ * queryKey: ['post', id],
170
+ * queryFn: () => fetchPost(id),
171
+ * })
172
+ *
173
+ * function Post({ id }: { id: string }) {
174
+ * const { data, isPending, isError, error } = useQuery(postOptions(id))
175
+ *
176
+ * if (isPending) return 'Loading...'
177
+ * if (isError) return <span>Error: {error.message}</span>
178
+ *
179
+ * return <h1>{data.title}</h1>
180
+ * }
181
+ * ```
182
+ */
62
183
  export function queryOptions<
63
184
  TQueryFnData = unknown,
64
185
  TError = DefaultError,
@@ -69,6 +190,60 @@ export function queryOptions<
69
190
  ): UnusedSkipTokenOptions<TQueryFnData, TError, TData, TQueryKey> &
70
191
  QueryKeyWithDataTag<TQueryKey, TQueryFnData, TError>
71
192
 
193
+ /**
194
+ * You can generally pass everything to `queryOptions` that you can also pass to `useQuery`. These options can
195
+ * be shared across hooks and imperative APIs such as `queryClient.query`. `options.queryKey` is required and
196
+ * is the query key to generate options for.
197
+ *
198
+ * @see {@link useQuery} to run a query with these options.
199
+ * @see [The Query Options API](https://tkdodo.eu/blog/the-query-options-api) for more on this pattern.
200
+ * @param options - The {@link UndefinedInitialDataOptions} to use — everything you can pass to `useQuery`.
201
+ * @returns The same options object, typed so that `queryKey` carries the inferred data type.
202
+ * @remarks This is the only overload that accepts `queryFn: skipToken`, shown below.
203
+ *
204
+ * @example
205
+ * A parameterized factory, so the same options object can be reused per `id`:
206
+ * ```tsx
207
+ * import { queryOptions, useQuery } from '@tanstack/react-query'
208
+ *
209
+ * export const postOptions = (id: string) =>
210
+ * queryOptions({
211
+ * queryKey: ['post', id],
212
+ * queryFn: () => fetchPost(id),
213
+ * })
214
+ *
215
+ * function Post({ id }: { id: string }) {
216
+ * const { data, isPending, isError, error } = useQuery(postOptions(id))
217
+ *
218
+ * if (isPending) return 'Loading...'
219
+ * if (isError) return <span>Error: {error.message}</span>
220
+ *
221
+ * return <h1>{data.title}</h1>
222
+ * }
223
+ * ```
224
+ *
225
+ * @example
226
+ * A factory that disables the query, type safe, until `postId` is set:
227
+ * ```tsx
228
+ * import { queryOptions, skipToken, useQuery } from '@tanstack/react-query'
229
+ *
230
+ * export const postOptions = (postId: number | undefined) =>
231
+ * queryOptions({
232
+ * queryKey: ['post', postId],
233
+ * queryFn: postId != null ? () => fetchPost(postId) : skipToken,
234
+ * })
235
+ *
236
+ * function Post({ postId }: { postId: number | undefined }) {
237
+ * const { data, isLoading, isError, error } = useQuery(postOptions(postId))
238
+ *
239
+ * if (postId == null) return 'Select a post'
240
+ * if (isLoading) return 'Loading...'
241
+ * if (isError) return <span>Error: {error.message}</span>
242
+ *
243
+ * return <h1>{data?.title}</h1>
244
+ * }
245
+ * ```
246
+ */
72
247
  export function queryOptions<
73
248
  TQueryFnData = unknown,
74
249
  TError = DefaultError,