@tanstack/preact-query 5.102.4 → 5.102.6

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 (125) hide show
  1. package/build/legacy/QueryErrorResetBoundary.cjs +11 -9
  2. package/build/legacy/QueryErrorResetBoundary.cjs.map +1 -1
  3. package/build/legacy/QueryErrorResetBoundary.d.cts +11 -9
  4. package/build/legacy/QueryErrorResetBoundary.d.cts.map +1 -1
  5. package/build/legacy/QueryErrorResetBoundary.d.ts +11 -9
  6. package/build/legacy/QueryErrorResetBoundary.d.ts.map +1 -1
  7. package/build/legacy/QueryErrorResetBoundary.js +11 -9
  8. package/build/legacy/QueryErrorResetBoundary.js.map +1 -1
  9. package/build/legacy/useInfiniteQuery.cjs.map +1 -1
  10. package/build/legacy/useInfiniteQuery.d.cts +28 -24
  11. package/build/legacy/useInfiniteQuery.d.cts.map +1 -1
  12. package/build/legacy/useInfiniteQuery.d.ts +28 -24
  13. package/build/legacy/useInfiniteQuery.d.ts.map +1 -1
  14. package/build/legacy/useInfiniteQuery.js.map +1 -1
  15. package/build/legacy/useIsFetching.cjs +14 -0
  16. package/build/legacy/useIsFetching.cjs.map +1 -1
  17. package/build/legacy/useIsFetching.d.cts +14 -0
  18. package/build/legacy/useIsFetching.d.cts.map +1 -1
  19. package/build/legacy/useIsFetching.d.ts +14 -0
  20. package/build/legacy/useIsFetching.d.ts.map +1 -1
  21. package/build/legacy/useIsFetching.js +14 -0
  22. package/build/legacy/useIsFetching.js.map +1 -1
  23. package/build/legacy/useMutation.cjs +104 -1
  24. package/build/legacy/useMutation.cjs.map +1 -1
  25. package/build/legacy/useMutation.d.cts +104 -1
  26. package/build/legacy/useMutation.d.cts.map +1 -1
  27. package/build/legacy/useMutation.d.ts +104 -1
  28. package/build/legacy/useMutation.d.ts.map +1 -1
  29. package/build/legacy/useMutation.js +104 -1
  30. package/build/legacy/useMutation.js.map +1 -1
  31. package/build/legacy/useQuery.cjs.map +1 -1
  32. package/build/legacy/useQuery.d.cts +54 -2
  33. package/build/legacy/useQuery.d.cts.map +1 -1
  34. package/build/legacy/useQuery.d.ts +54 -2
  35. package/build/legacy/useQuery.d.ts.map +1 -1
  36. package/build/legacy/useQuery.js.map +1 -1
  37. package/build/legacy/useSuspenseInfiniteQuery.cjs +16 -8
  38. package/build/legacy/useSuspenseInfiniteQuery.cjs.map +1 -1
  39. package/build/legacy/useSuspenseInfiniteQuery.d.cts +16 -8
  40. package/build/legacy/useSuspenseInfiniteQuery.d.cts.map +1 -1
  41. package/build/legacy/useSuspenseInfiniteQuery.d.ts +16 -8
  42. package/build/legacy/useSuspenseInfiniteQuery.d.ts.map +1 -1
  43. package/build/legacy/useSuspenseInfiniteQuery.js +16 -8
  44. package/build/legacy/useSuspenseInfiniteQuery.js.map +1 -1
  45. package/build/legacy/useSuspenseQueries.cjs.map +1 -1
  46. package/build/legacy/useSuspenseQueries.d.cts +74 -6
  47. package/build/legacy/useSuspenseQueries.d.cts.map +1 -1
  48. package/build/legacy/useSuspenseQueries.d.ts +74 -6
  49. package/build/legacy/useSuspenseQueries.d.ts.map +1 -1
  50. package/build/legacy/useSuspenseQueries.js.map +1 -1
  51. package/build/legacy/useSuspenseQuery.cjs +4 -0
  52. package/build/legacy/useSuspenseQuery.cjs.map +1 -1
  53. package/build/legacy/useSuspenseQuery.d.cts +4 -0
  54. package/build/legacy/useSuspenseQuery.d.cts.map +1 -1
  55. package/build/legacy/useSuspenseQuery.d.ts +4 -0
  56. package/build/legacy/useSuspenseQuery.d.ts.map +1 -1
  57. package/build/legacy/useSuspenseQuery.js +4 -0
  58. package/build/legacy/useSuspenseQuery.js.map +1 -1
  59. package/build/modern/QueryErrorResetBoundary.cjs +11 -9
  60. package/build/modern/QueryErrorResetBoundary.cjs.map +1 -1
  61. package/build/modern/QueryErrorResetBoundary.d.cts +11 -9
  62. package/build/modern/QueryErrorResetBoundary.d.cts.map +1 -1
  63. package/build/modern/QueryErrorResetBoundary.d.ts +11 -9
  64. package/build/modern/QueryErrorResetBoundary.d.ts.map +1 -1
  65. package/build/modern/QueryErrorResetBoundary.js +11 -9
  66. package/build/modern/QueryErrorResetBoundary.js.map +1 -1
  67. package/build/modern/useInfiniteQuery.cjs.map +1 -1
  68. package/build/modern/useInfiniteQuery.d.cts +28 -24
  69. package/build/modern/useInfiniteQuery.d.cts.map +1 -1
  70. package/build/modern/useInfiniteQuery.d.ts +28 -24
  71. package/build/modern/useInfiniteQuery.d.ts.map +1 -1
  72. package/build/modern/useInfiniteQuery.js.map +1 -1
  73. package/build/modern/useIsFetching.cjs +14 -0
  74. package/build/modern/useIsFetching.cjs.map +1 -1
  75. package/build/modern/useIsFetching.d.cts +14 -0
  76. package/build/modern/useIsFetching.d.cts.map +1 -1
  77. package/build/modern/useIsFetching.d.ts +14 -0
  78. package/build/modern/useIsFetching.d.ts.map +1 -1
  79. package/build/modern/useIsFetching.js +14 -0
  80. package/build/modern/useIsFetching.js.map +1 -1
  81. package/build/modern/useMutation.cjs +104 -1
  82. package/build/modern/useMutation.cjs.map +1 -1
  83. package/build/modern/useMutation.d.cts +104 -1
  84. package/build/modern/useMutation.d.cts.map +1 -1
  85. package/build/modern/useMutation.d.ts +104 -1
  86. package/build/modern/useMutation.d.ts.map +1 -1
  87. package/build/modern/useMutation.js +104 -1
  88. package/build/modern/useMutation.js.map +1 -1
  89. package/build/modern/useQuery.cjs.map +1 -1
  90. package/build/modern/useQuery.d.cts +54 -2
  91. package/build/modern/useQuery.d.cts.map +1 -1
  92. package/build/modern/useQuery.d.ts +54 -2
  93. package/build/modern/useQuery.d.ts.map +1 -1
  94. package/build/modern/useQuery.js.map +1 -1
  95. package/build/modern/useSuspenseInfiniteQuery.cjs +16 -8
  96. package/build/modern/useSuspenseInfiniteQuery.cjs.map +1 -1
  97. package/build/modern/useSuspenseInfiniteQuery.d.cts +16 -8
  98. package/build/modern/useSuspenseInfiniteQuery.d.cts.map +1 -1
  99. package/build/modern/useSuspenseInfiniteQuery.d.ts +16 -8
  100. package/build/modern/useSuspenseInfiniteQuery.d.ts.map +1 -1
  101. package/build/modern/useSuspenseInfiniteQuery.js +16 -8
  102. package/build/modern/useSuspenseInfiniteQuery.js.map +1 -1
  103. package/build/modern/useSuspenseQueries.cjs.map +1 -1
  104. package/build/modern/useSuspenseQueries.d.cts +74 -6
  105. package/build/modern/useSuspenseQueries.d.cts.map +1 -1
  106. package/build/modern/useSuspenseQueries.d.ts +74 -6
  107. package/build/modern/useSuspenseQueries.d.ts.map +1 -1
  108. package/build/modern/useSuspenseQueries.js.map +1 -1
  109. package/build/modern/useSuspenseQuery.cjs +4 -0
  110. package/build/modern/useSuspenseQuery.cjs.map +1 -1
  111. package/build/modern/useSuspenseQuery.d.cts +4 -0
  112. package/build/modern/useSuspenseQuery.d.cts.map +1 -1
  113. package/build/modern/useSuspenseQuery.d.ts +4 -0
  114. package/build/modern/useSuspenseQuery.d.ts.map +1 -1
  115. package/build/modern/useSuspenseQuery.js +4 -0
  116. package/build/modern/useSuspenseQuery.js.map +1 -1
  117. package/package.json +3 -3
  118. package/src/QueryErrorResetBoundary.tsx +11 -9
  119. package/src/useInfiniteQuery.ts +28 -24
  120. package/src/useIsFetching.ts +14 -0
  121. package/src/useMutation.ts +104 -1
  122. package/src/useQuery.ts +54 -2
  123. package/src/useSuspenseInfiniteQuery.ts +16 -8
  124. package/src/useSuspenseQueries.ts +74 -6
  125. package/src/useSuspenseQuery.ts +4 -0
@@ -27,7 +27,45 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
27
27
  * })
28
28
  *
29
29
  * return (
30
- * <button onClick={() => addMutation.mutate('Item')}>Add</button>
30
+ * <button
31
+ * onClick={() =>
32
+ * addMutation.mutate('Item', {
33
+ * onError: (error) => console.error('Failed to add item:', error),
34
+ * })
35
+ * }
36
+ * >
37
+ * Add
38
+ * </button>
39
+ * )
40
+ * }
41
+ * ```
42
+ *
43
+ * @example
44
+ * Rendering the mutation's own state, rather than just firing it off:
45
+ * ```tsx
46
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
47
+ *
48
+ * function AddTodo() {
49
+ * const queryClient = useQueryClient()
50
+ *
51
+ * const addMutation = useMutation({
52
+ * mutationFn: addTodo,
53
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
54
+ * })
55
+ *
56
+ * return (
57
+ * <div>
58
+ * {addMutation.isPending ? (
59
+ * 'Adding todo...'
60
+ * ) : (
61
+ * <>
62
+ * {addMutation.isError ? (
63
+ * <div>An error occurred: {addMutation.error.message}</div>
64
+ * ) : null}
65
+ * <button onClick={() => addMutation.mutate('Item')}>Add</button>
66
+ * </>
67
+ * )}
68
+ * </div>
31
69
  * )
32
70
  * }
33
71
  * ```
@@ -67,6 +105,71 @@ import { DefaultError, QueryClient } from "@tanstack/query-core";
67
105
  * )
68
106
  * }
69
107
  * ```
108
+ *
109
+ * @example
110
+ * Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
111
+ * promise per call instead, so you can wait for all of them:
112
+ * ```tsx
113
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
114
+ *
115
+ * function AddTodos() {
116
+ * const queryClient = useQueryClient()
117
+ *
118
+ * const addMutation = useMutation({
119
+ * mutationFn: addTodo,
120
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
121
+ * })
122
+ *
123
+ * async function handleAddAll(todos: Array<string>) {
124
+ * try {
125
+ * await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
126
+ * } catch (error) {
127
+ * console.error('Failed to add todos:', error)
128
+ * }
129
+ * }
130
+ *
131
+ * return (
132
+ * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
133
+ * Add all
134
+ * </button>
135
+ * )
136
+ * }
137
+ * ```
138
+ *
139
+ * @example
140
+ * If some of the mutations above can fail independently of the others, and you want to know which ones
141
+ * did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
142
+ * `Promise.allSettled`:
143
+ * ```tsx
144
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
145
+ *
146
+ * function AddTodos() {
147
+ * const queryClient = useQueryClient()
148
+ *
149
+ * const addMutation = useMutation({
150
+ * mutationFn: addTodo,
151
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
152
+ * })
153
+ *
154
+ * async function handleAddAll(todos: Array<string>) {
155
+ * const results = await Promise.allSettled(
156
+ * todos.map((todo) => addMutation.mutateAsync(todo)),
157
+ * )
158
+ *
159
+ * results.forEach((result, index) => {
160
+ * if (result.status === 'rejected') {
161
+ * console.error(`Failed to add "${todos[index]}":`, result.reason)
162
+ * }
163
+ * })
164
+ * }
165
+ *
166
+ * return (
167
+ * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
168
+ * Add all
169
+ * </button>
170
+ * )
171
+ * }
172
+ * ```
70
173
  */
71
174
  declare function useMutation<TData = unknown, TError = DefaultError, TVariables = void, TOnMutateResult = unknown>(options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>, queryClient?: QueryClient): UseMutationResult<TData, TError, TVariables, TOnMutateResult>;
72
175
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useMutation.d.ts","names":[],"sources":["../../src/useMutation.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAsFgB,YACd,iBACA,SAAS,cACT,mBACA,2BAEA,SAAS,mBAAmB,OAAO,QAAQ,YAAY,kBACvD,cAAc,cACb,kBAAkB,OAAO,QAAQ,YAAY"}
1
+ {"version":3,"file":"useMutation.d.ts","names":[],"sources":["../../src/useMutation.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA6LgB,YACd,iBACA,SAAS,cACT,mBACA,2BAEA,SAAS,mBAAmB,OAAO,QAAQ,YAAY,kBACvD,cAAc,cACb,kBAAkB,OAAO,QAAQ,YAAY"}
@@ -29,7 +29,45 @@ import { useCallback, useEffect, useState } from "preact/hooks";
29
29
  * })
30
30
  *
31
31
  * return (
32
- * <button onClick={() => addMutation.mutate('Item')}>Add</button>
32
+ * <button
33
+ * onClick={() =>
34
+ * addMutation.mutate('Item', {
35
+ * onError: (error) => console.error('Failed to add item:', error),
36
+ * })
37
+ * }
38
+ * >
39
+ * Add
40
+ * </button>
41
+ * )
42
+ * }
43
+ * ```
44
+ *
45
+ * @example
46
+ * Rendering the mutation's own state, rather than just firing it off:
47
+ * ```tsx
48
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
49
+ *
50
+ * function AddTodo() {
51
+ * const queryClient = useQueryClient()
52
+ *
53
+ * const addMutation = useMutation({
54
+ * mutationFn: addTodo,
55
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
56
+ * })
57
+ *
58
+ * return (
59
+ * <div>
60
+ * {addMutation.isPending ? (
61
+ * 'Adding todo...'
62
+ * ) : (
63
+ * <>
64
+ * {addMutation.isError ? (
65
+ * <div>An error occurred: {addMutation.error.message}</div>
66
+ * ) : null}
67
+ * <button onClick={() => addMutation.mutate('Item')}>Add</button>
68
+ * </>
69
+ * )}
70
+ * </div>
33
71
  * )
34
72
  * }
35
73
  * ```
@@ -69,6 +107,71 @@ import { useCallback, useEffect, useState } from "preact/hooks";
69
107
  * )
70
108
  * }
71
109
  * ```
110
+ *
111
+ * @example
112
+ * Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a
113
+ * promise per call instead, so you can wait for all of them:
114
+ * ```tsx
115
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
116
+ *
117
+ * function AddTodos() {
118
+ * const queryClient = useQueryClient()
119
+ *
120
+ * const addMutation = useMutation({
121
+ * mutationFn: addTodo,
122
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
123
+ * })
124
+ *
125
+ * async function handleAddAll(todos: Array<string>) {
126
+ * try {
127
+ * await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))
128
+ * } catch (error) {
129
+ * console.error('Failed to add todos:', error)
130
+ * }
131
+ * }
132
+ *
133
+ * return (
134
+ * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
135
+ * Add all
136
+ * </button>
137
+ * )
138
+ * }
139
+ * ```
140
+ *
141
+ * @example
142
+ * If some of the mutations above can fail independently of the others, and you want to know which ones
143
+ * did — rather than losing that information the moment the first one rejects — swap `Promise.all` for
144
+ * `Promise.allSettled`:
145
+ * ```tsx
146
+ * import { useMutation, useQueryClient } from '@tanstack/preact-query'
147
+ *
148
+ * function AddTodos() {
149
+ * const queryClient = useQueryClient()
150
+ *
151
+ * const addMutation = useMutation({
152
+ * mutationFn: addTodo,
153
+ * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),
154
+ * })
155
+ *
156
+ * async function handleAddAll(todos: Array<string>) {
157
+ * const results = await Promise.allSettled(
158
+ * todos.map((todo) => addMutation.mutateAsync(todo)),
159
+ * )
160
+ *
161
+ * results.forEach((result, index) => {
162
+ * if (result.status === 'rejected') {
163
+ * console.error(`Failed to add "${todos[index]}":`, result.reason)
164
+ * }
165
+ * })
166
+ * }
167
+ *
168
+ * return (
169
+ * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>
170
+ * Add all
171
+ * </button>
172
+ * )
173
+ * }
174
+ * ```
72
175
  */
73
176
  function useMutation(options, queryClient) {
74
177
  const client = useQueryClient(queryClient);
@@ -1 +1 @@
1
- {"version":3,"file":"useMutation.js","names":[],"sources":["../../src/useMutation.ts"],"sourcesContent":["import {\n MutationObserver,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport type { DefaultError, QueryClient } from '@tanstack/query-core'\nimport { useCallback, useEffect, useState } from 'preact/hooks'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n UseMutateFunction,\n UseMutationOptions,\n UseMutationResult,\n} from './types'\nimport { useSyncExternalStore } from './utils'\n\n// HOOK\n\n/**\n * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.\n * `useMutation` is the hook for that.\n *\n * @see {@link mutationOptions} to share these options across multiple `useMutation` call sites.\n * @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second\n * argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared\n * mutation definition. If you make multiple requests, `onSuccess` will fire only after the latest call you've\n * made.\n *\n * @example\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * )\n * }\n * ```\n *\n * @example\n * Optimistic update via `onMutate`, rolling back on `onError`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onMutate: async (newTodo) => {\n * await queryClient.cancelQueries({ queryKey: ['todos'] })\n * const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])\n *\n * queryClient.setQueryData<Array<string>>(['todos'], (old) => [\n * ...(old ?? []),\n * newTodo,\n * ])\n *\n * // Passed to `onError` as `context` if the mutation fails.\n * return { previousTodos }\n * },\n * onError: (_err, _newTodo, context) => {\n * queryClient.setQueryData(['todos'], context?.previousTodos)\n * },\n * onSettled: () => {\n * queryClient.invalidateQueries({ queryKey: ['todos'] })\n * },\n * })\n *\n * return (\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * )\n * }\n * ```\n */\nexport function useMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n>(\n options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n queryClient?: QueryClient,\n): UseMutationResult<TData, TError, TVariables, TOnMutateResult> {\n const client = useQueryClient(queryClient)\n\n const [observer] = useState(\n () =>\n new MutationObserver<TData, TError, TVariables, TOnMutateResult>(\n client,\n options,\n ),\n )\n\n useEffect(() => {\n observer.setOptions(options)\n }, [observer, options])\n\n const result = useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer],\n ),\n () => observer.getCurrentResult(),\n )\n\n const mutate = useCallback<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >(\n (\n ...args: Parameters<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >\n ) => {\n observer.mutate(args[0] as TVariables, args[1]).catch(noop)\n },\n [observer],\n )\n\n if (\n result.error &&\n shouldThrowError(observer.options.throwOnError, [result.error])\n ) {\n throw result.error\n }\n\n return { ...result, mutate, mutateAsync: result.mutate }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAsFA,SAAgB,YAMd,SACA,aAC+D;CAC/D,MAAM,SAAS,eAAe,WAAW;CAEzC,MAAM,CAAC,YAAY,eAEf,IAAI,iBACF,QACA,OACF,CACJ;CAEA,gBAAgB;EACd,SAAS,WAAW,OAAO;CAC7B,GAAG,CAAC,UAAU,OAAO,CAAC;CAEtB,MAAM,SAAS,qBACb,aACG,kBACC,SAAS,UAAU,cAAc,WAAW,aAAa,CAAC,GAC5D,CAAC,QAAQ,CACX,SACM,SAAS,iBAAiB,CAClC;CAEA,MAAM,SAAS,aAIX,GAAG,SAGA;EACH,SAAS,OAAO,KAAK,IAAkB,KAAK,EAAE,CAAC,CAAC,MAAM,IAAI;CAC5D,GACA,CAAC,QAAQ,CACX;CAEA,IACE,OAAO,SACP,iBAAiB,SAAS,QAAQ,cAAc,CAAC,OAAO,KAAK,CAAC,GAE9D,MAAM,OAAO;CAGf,OAAO;EAAE,GAAG;EAAQ;EAAQ,aAAa,OAAO;CAAO;AACzD"}
1
+ {"version":3,"file":"useMutation.js","names":[],"sources":["../../src/useMutation.ts"],"sourcesContent":["import {\n MutationObserver,\n noop,\n notifyManager,\n shouldThrowError,\n} from '@tanstack/query-core'\nimport type { DefaultError, QueryClient } from '@tanstack/query-core'\nimport { useCallback, useEffect, useState } from 'preact/hooks'\n\nimport { useQueryClient } from './QueryClientProvider'\nimport type {\n UseMutateFunction,\n UseMutationOptions,\n UseMutationResult,\n} from './types'\nimport { useSyncExternalStore } from './utils'\n\n// HOOK\n\n/**\n * Unlike queries, mutations are typically used to create/update/delete data or perform server side-effects.\n * `useMutation` is the hook for that.\n *\n * @see {@link mutationOptions} to share these options across multiple `useMutation` call sites.\n * @param options - The {@link UseMutationOptions} to use — everything you can pass to `useMutation`.\n * @param queryClient - Use this to use a custom `QueryClient`. Otherwise, the one from the nearest context will\n * be used.\n * @returns `mutate`/`mutateAsync` also accept per-call `onSuccess`/`onError`/`onSettled` callbacks as a second\n * argument, useful for triggering call-site side effects (e.g. navigation) without coupling them to the shared\n * mutation definition. If you make multiple requests, `onSuccess` will fire only after the latest call you've\n * made.\n *\n * @example\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <button\n * onClick={() =>\n * addMutation.mutate('Item', {\n * onError: (error) => console.error('Failed to add item:', error),\n * })\n * }\n * >\n * Add\n * </button>\n * )\n * }\n * ```\n *\n * @example\n * Rendering the mutation's own state, rather than just firing it off:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * return (\n * <div>\n * {addMutation.isPending ? (\n * 'Adding todo...'\n * ) : (\n * <>\n * {addMutation.isError ? (\n * <div>An error occurred: {addMutation.error.message}</div>\n * ) : null}\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * </>\n * )}\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * Optimistic update via `onMutate`, rolling back on `onError`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodo() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onMutate: async (newTodo) => {\n * await queryClient.cancelQueries({ queryKey: ['todos'] })\n * const previousTodos = queryClient.getQueryData<Array<string>>(['todos'])\n *\n * queryClient.setQueryData<Array<string>>(['todos'], (old) => [\n * ...(old ?? []),\n * newTodo,\n * ])\n *\n * // Passed to `onError` as `context` if the mutation fails.\n * return { previousTodos }\n * },\n * onError: (_err, _newTodo, context) => {\n * queryClient.setQueryData(['todos'], context?.previousTodos)\n * },\n * onSettled: () => {\n * queryClient.invalidateQueries({ queryKey: ['todos'] })\n * },\n * })\n *\n * return (\n * <button onClick={() => addMutation.mutate('Item')}>Add</button>\n * )\n * }\n * ```\n *\n * @example\n * Callbacks passed per call to `mutate` only fire for the last call — `mutateAsync` gives you a\n * promise per call instead, so you can wait for all of them:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodos() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * async function handleAddAll(todos: Array<string>) {\n * try {\n * await Promise.all(todos.map((todo) => addMutation.mutateAsync(todo)))\n * } catch (error) {\n * console.error('Failed to add todos:', error)\n * }\n * }\n *\n * return (\n * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>\n * Add all\n * </button>\n * )\n * }\n * ```\n *\n * @example\n * If some of the mutations above can fail independently of the others, and you want to know which ones\n * did — rather than losing that information the moment the first one rejects — swap `Promise.all` for\n * `Promise.allSettled`:\n * ```tsx\n * import { useMutation, useQueryClient } from '@tanstack/preact-query'\n *\n * function AddTodos() {\n * const queryClient = useQueryClient()\n *\n * const addMutation = useMutation({\n * mutationFn: addTodo,\n * onSuccess: () => queryClient.invalidateQueries({ queryKey: ['todos'] }),\n * })\n *\n * async function handleAddAll(todos: Array<string>) {\n * const results = await Promise.allSettled(\n * todos.map((todo) => addMutation.mutateAsync(todo)),\n * )\n *\n * results.forEach((result, index) => {\n * if (result.status === 'rejected') {\n * console.error(`Failed to add \"${todos[index]}\":`, result.reason)\n * }\n * })\n * }\n *\n * return (\n * <button onClick={() => handleAddAll(['Todo 1', 'Todo 2', 'Todo 3'])}>\n * Add all\n * </button>\n * )\n * }\n * ```\n */\nexport function useMutation<\n TData = unknown,\n TError = DefaultError,\n TVariables = void,\n TOnMutateResult = unknown,\n>(\n options: UseMutationOptions<TData, TError, TVariables, TOnMutateResult>,\n queryClient?: QueryClient,\n): UseMutationResult<TData, TError, TVariables, TOnMutateResult> {\n const client = useQueryClient(queryClient)\n\n const [observer] = useState(\n () =>\n new MutationObserver<TData, TError, TVariables, TOnMutateResult>(\n client,\n options,\n ),\n )\n\n useEffect(() => {\n observer.setOptions(options)\n }, [observer, options])\n\n const result = useSyncExternalStore(\n useCallback(\n (onStoreChange) =>\n observer.subscribe(notifyManager.batchCalls(onStoreChange)),\n [observer],\n ),\n () => observer.getCurrentResult(),\n )\n\n const mutate = useCallback<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >(\n (\n ...args: Parameters<\n UseMutateFunction<TData, TError, TVariables, TOnMutateResult>\n >\n ) => {\n observer.mutate(args[0] as TVariables, args[1]).catch(noop)\n },\n [observer],\n )\n\n if (\n result.error &&\n shouldThrowError(observer.options.throwOnError, [result.error])\n ) {\n throw result.error\n }\n\n return { ...result, mutate, mutateAsync: result.mutate }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6LA,SAAgB,YAMd,SACA,aAC+D;CAC/D,MAAM,SAAS,eAAe,WAAW;CAEzC,MAAM,CAAC,YAAY,eAEf,IAAI,iBACF,QACA,OACF,CACJ;CAEA,gBAAgB;EACd,SAAS,WAAW,OAAO;CAC7B,GAAG,CAAC,UAAU,OAAO,CAAC;CAEtB,MAAM,SAAS,qBACb,aACG,kBACC,SAAS,UAAU,cAAc,WAAW,aAAa,CAAC,GAC5D,CAAC,QAAQ,CACX,SACM,SAAS,iBAAiB,CAClC;CAEA,MAAM,SAAS,aAIX,GAAG,SAGA;EACH,SAAS,OAAO,KAAK,IAAkB,KAAK,EAAE,CAAC,CAAC,MAAM,IAAI;CAC5D,GACA,CAAC,QAAQ,CACX;CAEA,IACE,OAAO,SACP,iBAAiB,SAAS,QAAQ,cAAc,CAAC,OAAO,KAAK,CAAC,GAE9D,MAAM,OAAO;CAGf,OAAO;EAAE,GAAG;EAAQ;EAAQ,aAAa,OAAO;CAAO;AACzD"}
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.cjs","names":["useBaseQuery","QueryObserver"],"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`.\n * const { data } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n *\n * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\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: 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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * A dependent query, only enabled once `postId` is set:\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number | undefined }) {\n * const { data } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId!),\n * enabled: postId != null,\n * })\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 } = 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 * return <h1>{data?.title}</h1>\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":";;;;AAkLA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAOA,qBAAAA,aAAa,SAASC,qBAAAA,eAAe,WAAW;AACzD"}
1
+ {"version":3,"file":"useQuery.cjs","names":["useBaseQuery","QueryObserver"],"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`.\n * const { data } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n *\n * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\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 <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\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 * 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 } = 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 * 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 } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page),\n * placeholderData: keepPreviousData,\n * })\n *\n * return (\n * <div>\n * {data?.map((post) => <p key={post.id}>{post.title}</p>)}\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":";;;;AAsOA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAOA,qBAAAA,aAAa,SAASC,qBAAAA,eAAe,WAAW;AACzD"}
@@ -64,6 +64,24 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
64
64
  * )
65
65
  * }
66
66
  * ```
67
+ *
68
+ * @example
69
+ * The same query, checking `isPending`/`isError` instead of `status` — pick whichever reads better to you:
70
+ * ```tsx
71
+ * import { useQuery } from '@tanstack/preact-query'
72
+ *
73
+ * function Posts() {
74
+ * const { isPending, isError, data, error } = useQuery({
75
+ * queryKey: ['posts'],
76
+ * queryFn: fetchPosts,
77
+ * })
78
+ *
79
+ * if (isPending) return 'Loading...'
80
+ * if (isError) return <span>Error: {error.message}</span>
81
+ *
82
+ * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>
83
+ * }
84
+ * ```
67
85
  */
68
86
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
69
87
  /**
@@ -102,17 +120,22 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
102
120
  * ```
103
121
  *
104
122
  * @example
105
- * A dependent query, only enabled once `postId` is set:
123
+ * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the
124
+ * loading state doesn't show while the query is disabled:
106
125
  * ```tsx
107
126
  * import { useQuery } from '@tanstack/preact-query'
108
127
  *
109
128
  * function Post({ postId }: { postId: number | undefined }) {
110
- * const { data } = useQuery({
129
+ * const { data, isLoading, isError, error } = useQuery({
111
130
  * queryKey: ['post', postId],
112
131
  * queryFn: () => fetchPost(postId!),
113
132
  * enabled: postId != null,
114
133
  * })
115
134
  *
135
+ * if (postId == null) return 'Select a post'
136
+ * if (isLoading) return 'Loading...'
137
+ * if (isError) return <span>Error: {error.message}</span>
138
+ *
116
139
  * return <h1>{data?.title}</h1>
117
140
  * }
118
141
  * ```
@@ -137,6 +160,35 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
137
160
  * return <h1>{data?.title}</h1>
138
161
  * }
139
162
  * ```
163
+ *
164
+ * @example
165
+ * Paginated data, keeping the previous page's data visible while the next page loads:
166
+ * ```tsx
167
+ * import { keepPreviousData, useQuery } from '@tanstack/preact-query'
168
+ * import { useState } from 'preact/hooks'
169
+ *
170
+ * function Posts() {
171
+ * const [page, setPage] = useState(0)
172
+ *
173
+ * const { data, isPlaceholderData } = useQuery({
174
+ * queryKey: ['posts', page],
175
+ * queryFn: () => fetchPosts(page),
176
+ * placeholderData: keepPreviousData,
177
+ * })
178
+ *
179
+ * return (
180
+ * <div>
181
+ * {data?.map((post) => <p key={post.id}>{post.title}</p>)}
182
+ * <button
183
+ * disabled={isPlaceholderData}
184
+ * onClick={() => setPage((old) => old + 1)}
185
+ * >
186
+ * Next Page
187
+ * </button>
188
+ * </div>
189
+ * )
190
+ * }
191
+ * ```
140
192
  */
141
193
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
142
194
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.d.cts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqChB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0ET,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}
1
+ {"version":3,"file":"useQuery.d.cts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuDhB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4GT,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}
@@ -64,6 +64,24 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
64
64
  * )
65
65
  * }
66
66
  * ```
67
+ *
68
+ * @example
69
+ * The same query, checking `isPending`/`isError` instead of `status` — pick whichever reads better to you:
70
+ * ```tsx
71
+ * import { useQuery } from '@tanstack/preact-query'
72
+ *
73
+ * function Posts() {
74
+ * const { isPending, isError, data, error } = useQuery({
75
+ * queryKey: ['posts'],
76
+ * queryFn: fetchPosts,
77
+ * })
78
+ *
79
+ * if (isPending) return 'Loading...'
80
+ * if (isError) return <span>Error: {error.message}</span>
81
+ *
82
+ * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>
83
+ * }
84
+ * ```
67
85
  */
68
86
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UndefinedInitialDataOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
69
87
  /**
@@ -102,17 +120,22 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
102
120
  * ```
103
121
  *
104
122
  * @example
105
- * A dependent query, only enabled once `postId` is set:
123
+ * A dependent query, only enabled once `postId` is set — use `isLoading`, not `isPending`, so the
124
+ * loading state doesn't show while the query is disabled:
106
125
  * ```tsx
107
126
  * import { useQuery } from '@tanstack/preact-query'
108
127
  *
109
128
  * function Post({ postId }: { postId: number | undefined }) {
110
- * const { data } = useQuery({
129
+ * const { data, isLoading, isError, error } = useQuery({
111
130
  * queryKey: ['post', postId],
112
131
  * queryFn: () => fetchPost(postId!),
113
132
  * enabled: postId != null,
114
133
  * })
115
134
  *
135
+ * if (postId == null) return 'Select a post'
136
+ * if (isLoading) return 'Loading...'
137
+ * if (isError) return <span>Error: {error.message}</span>
138
+ *
116
139
  * return <h1>{data?.title}</h1>
117
140
  * }
118
141
  * ```
@@ -137,6 +160,35 @@ declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData =
137
160
  * return <h1>{data?.title}</h1>
138
161
  * }
139
162
  * ```
163
+ *
164
+ * @example
165
+ * Paginated data, keeping the previous page's data visible while the next page loads:
166
+ * ```tsx
167
+ * import { keepPreviousData, useQuery } from '@tanstack/preact-query'
168
+ * import { useState } from 'preact/hooks'
169
+ *
170
+ * function Posts() {
171
+ * const [page, setPage] = useState(0)
172
+ *
173
+ * const { data, isPlaceholderData } = useQuery({
174
+ * queryKey: ['posts', page],
175
+ * queryFn: () => fetchPosts(page),
176
+ * placeholderData: keepPreviousData,
177
+ * })
178
+ *
179
+ * return (
180
+ * <div>
181
+ * {data?.map((post) => <p key={post.id}>{post.title}</p>)}
182
+ * <button
183
+ * disabled={isPlaceholderData}
184
+ * onClick={() => setPage((old) => old + 1)}
185
+ * >
186
+ * Next Page
187
+ * </button>
188
+ * </div>
189
+ * )
190
+ * }
191
+ * ```
140
192
  */
141
193
  declare function useQuery<TQueryFnData = unknown, TError = DefaultError, TData = TQueryFnData, TQueryKey extends QueryKey = QueryKey>(options: UseQueryOptions<TQueryFnData, TError, TData, TQueryKey>, queryClient?: QueryClient): UseQueryResult<TData, TError>;
142
194
  //#endregion
@@ -1 +1 @@
1
- {"version":3,"file":"useQuery.d.ts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAqChB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA0ET,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,gBAAgB,cAAc,QAAQ,OAAO,YACtD,cAAc,cACb,eAAe,OAAO"}
1
+ {"version":3,"file":"useQuery.d.ts","names":[],"sources":["../../src/useQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyCgB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,0BAA0B,cAAc,QAAQ,OAAO,YAChE,cAAc,cACb,sBAAsB,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAuDhB,SACd,wBACA,SAAS,cACT,QAAQ,cACR,kBAAkB,WAAW,UAE7B,SAAS,4BAA4B,cAAc,QAAQ,OAAO,YAClE,cAAc,cACb,eAAe,OAAO;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBA4GT,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`.\n * const { data } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n *\n * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\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: 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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\n * }\n * ```\n *\n * @example\n * A dependent query, only enabled once `postId` is set:\n * ```tsx\n * import { useQuery } from '@tanstack/preact-query'\n *\n * function Post({ postId }: { postId: number | undefined }) {\n * const { data } = useQuery({\n * queryKey: ['post', postId],\n * queryFn: () => fetchPost(postId!),\n * enabled: postId != null,\n * })\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 } = 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 * return <h1>{data?.title}</h1>\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":";;;AAkLA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAO,aAAa,SAAS,eAAe,WAAW;AACzD"}
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`.\n * const { data } = useQuery({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * initialData: [],\n * })\n *\n * return <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\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 <>{data.map((post) => <p key={post.id}>{post.title}</p>)}</>\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 and no query attempt\n * has finished yet, `error` if the query attempt resulted in an error, or `success` if the query has data to\n * display. `isPending`/`isSuccess`/`isError` are derived booleans for convenience.\n *\n * @example\n * ```tsx\n * import { queryOptions, useQuery } from '@tanstack/preact-query'\n *\n * const postsOptions = queryOptions({\n * queryKey: ['posts'],\n * queryFn: fetchPosts,\n * })\n *\n * function Posts() {\n * const { status, data, error, isFetching } = useQuery(postsOptions)\n *\n * if (status === 'pending') return 'Loading...'\n * if (status === 'error') return <span>Error: {error.message}</span>\n *\n * return (\n * <div>\n * {data.map((post) => (\n * <p key={post.id}>{post.title}</p>\n * ))}\n * <div>{isFetching ? 'Background Updating...' : ' '}</div>\n * </div>\n * )\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 * 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 } = 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 * 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 } = useQuery({\n * queryKey: ['posts', page],\n * queryFn: () => fetchPosts(page),\n * placeholderData: keepPreviousData,\n * })\n *\n * return (\n * <div>\n * {data?.map((post) => <p key={post.id}>{post.title}</p>)}\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":";;;AAsOA,SAAgB,SAAS,SAA0B,aAA2B;CAC5E,OAAO,aAAa,SAAS,eAAe,WAAW;AACzD"}
@@ -23,20 +23,28 @@ let _tanstack_query_core = require("@tanstack/query-core");
23
23
  *
24
24
  * function Projects() {
25
25
  * // `data` is guaranteed to be defined here — no `isPending` check needed.
26
- * const { data, fetchNextPage, hasNextPage } = useSuspenseInfiniteQuery({
27
- * queryKey: ['projects'],
28
- * queryFn: ({ pageParam }) => fetchProjects(pageParam),
29
- * initialPageParam: 0,
30
- * getNextPageParam: (lastPage) => lastPage.nextId,
31
- * })
26
+ * const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
27
+ * useSuspenseInfiniteQuery({
28
+ * queryKey: ['projects'],
29
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
30
+ * initialPageParam: 0,
31
+ * getNextPageParam: (lastPage) => lastPage.nextId,
32
+ * })
32
33
  *
33
34
  * return (
34
35
  * <div>
35
36
  * {data.pages.map((page) =>
36
37
  * page.projects.map((project) => <p key={project.id}>{project.name}</p>),
37
38
  * )}
38
- * <button onClick={() => fetchNextPage()} disabled={!hasNextPage}>
39
- * Load More
39
+ * <button
40
+ * onClick={() => fetchNextPage()}
41
+ * disabled={!hasNextPage || isFetching}
42
+ * >
43
+ * {isFetchingNextPage
44
+ * ? 'Loading more...'
45
+ * : hasNextPage
46
+ * ? 'Load More'
47
+ * : 'Nothing more to load'}
40
48
  * </button>
41
49
  * </div>
42
50
  * )
@@ -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 } = 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 * page.projects.map((project) => <p key={project.id}>{project.name}</p>),\n * )}\n * <button onClick={() => fetchNextPage()} disabled={!hasNextPage}>\n * Load More\n * </button>\n * </div>\n * )\n * }\n *\n * function App() {\n * return (\n * <Suspense fallback={<h1>Loading projects...</h1>}>\n * <Projects />\n * </Suspense>\n * )\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAiEA,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"}
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 * page.projects.map((project) => <p key={project.id}>{project.name}</p>),\n * )}\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 * <Suspense fallback={<h1>Loading projects...</h1>}>\n * <Projects />\n * </Suspense>\n * )\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":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAyEA,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"}
@@ -21,20 +21,28 @@ import { DefaultError, InfiniteData, QueryClient, QueryKey } from "@tanstack/que
21
21
  *
22
22
  * function Projects() {
23
23
  * // `data` is guaranteed to be defined here — no `isPending` check needed.
24
- * const { data, fetchNextPage, hasNextPage } = useSuspenseInfiniteQuery({
25
- * queryKey: ['projects'],
26
- * queryFn: ({ pageParam }) => fetchProjects(pageParam),
27
- * initialPageParam: 0,
28
- * getNextPageParam: (lastPage) => lastPage.nextId,
29
- * })
24
+ * const { data, fetchNextPage, hasNextPage, isFetching, isFetchingNextPage } =
25
+ * useSuspenseInfiniteQuery({
26
+ * queryKey: ['projects'],
27
+ * queryFn: ({ pageParam }) => fetchProjects(pageParam),
28
+ * initialPageParam: 0,
29
+ * getNextPageParam: (lastPage) => lastPage.nextId,
30
+ * })
30
31
  *
31
32
  * return (
32
33
  * <div>
33
34
  * {data.pages.map((page) =>
34
35
  * page.projects.map((project) => <p key={project.id}>{project.name}</p>),
35
36
  * )}
36
- * <button onClick={() => fetchNextPage()} disabled={!hasNextPage}>
37
- * Load More
37
+ * <button
38
+ * onClick={() => fetchNextPage()}
39
+ * disabled={!hasNextPage || isFetching}
40
+ * >
41
+ * {isFetchingNextPage
42
+ * ? 'Loading more...'
43
+ * : hasNextPage
44
+ * ? 'Load More'
45
+ * : 'Nothing more to load'}
38
46
  * </button>
39
47
  * </div>
40
48
  * )
@@ -1 +1 @@
1
- {"version":3,"file":"useSuspenseInfiniteQuery.d.cts","names":[],"sources":["../../src/useSuspenseInfiniteQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAiEgB,yBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,gCACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,+BAA+B,OAAO"}
1
+ {"version":3,"file":"useSuspenseInfiniteQuery.d.cts","names":[],"sources":["../../src/useSuspenseInfiniteQuery.ts"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBAyEgB,yBACd,cACA,SAAS,cACT,QAAQ,aAAa,eACrB,kBAAkB,WAAW,UAC7B,sBAEA,SAAS,gCACP,cACA,QACA,OACA,WACA,aAEF,cAAc,cACb,+BAA+B,OAAO"}