MCPcopy Create free account
hub / github.com/TanStack/query / useMutationState

Function useMutationState

packages/preact-query/src/useMutationState.ts:157–193  ·  view source on GitHub ↗
(
  options: MutationStateOptions<TResult, TMutation> = {},
  queryClient?: QueryClient,
)

Source from the content-addressed store, hash-verified

155 * ```
156 */
157export function useMutationState<
158 TResult = MutationState,
159 TMutation extends Mutation<any, any, any, any> =
160 MutationTypeFromResult<TResult>,
161>(
162 options: MutationStateOptions<TResult, TMutation> = {},
163 queryClient?: QueryClient,
164): Array<TResult> {
165 const mutationCache = useQueryClient(queryClient).getMutationCache()
166 const optionsRef = useRef(options)
167 const result = useRef<Array<TResult>>(null)
168 if (result.current === null) {
169 result.current = getResult(mutationCache, options)
170 }
171
172 useEffect(() => {
173 optionsRef.current = options
174 })
175
176 return useSyncExternalStore(
177 useCallback(
178 (onStoreChange) =>
179 mutationCache.subscribe(() => {
180 const nextResult = replaceEqualDeep(
181 result.current,
182 getResult(mutationCache, optionsRef.current),
183 )
184 if (result.current !== nextResult) {
185 result.current = nextResult
186 notifyManager.schedule(onStoreChange)
187 }
188 }),
189 [mutationCache],
190 ),
191 () => result.current,
192 )!
193}

Callers 6

useIsMutatingFunction · 0.70
VariablesFunction · 0.50
PageFunction · 0.50
MutationFunction · 0.50

Calls 6

useQueryClientFunction · 0.90
useSyncExternalStoreFunction · 0.90
replaceEqualDeepFunction · 0.90
getMutationCacheMethod · 0.80
getResultFunction · 0.70
subscribeMethod · 0.45

Tested by 3

VariablesFunction · 0.40
PageFunction · 0.40
MutationFunction · 0.40