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

Function useMutationState

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

Source from the content-addressed store, hash-verified

61}
62
63export function useMutationState<
64 TResult = MutationState,
65 TMutation extends Mutation<any, any, any, any> =
66 MutationTypeFromResult<TResult>,
67>(
68 options: MutationStateOptions<TResult, TMutation> = {},
69 queryClient?: QueryClient,
70): Array<TResult> {
71 const mutationCache = useQueryClient(queryClient).getMutationCache()
72 const optionsRef = React.useRef(options)
73 const result = React.useRef<Array<TResult>>(null)
74 if (result.current === null) {
75 result.current = getResult(mutationCache, options)
76 }
77
78 React.useEffect(() => {
79 optionsRef.current = options
80 })
81
82 return React.useSyncExternalStore(
83 React.useCallback(
84 (onStoreChange) =>
85 mutationCache.subscribe(() => {
86 const nextResult = replaceEqualDeep(
87 result.current,
88 getResult(mutationCache, optionsRef.current),
89 )
90 if (result.current !== nextResult) {
91 result.current = nextResult
92 notifyManager.schedule(onStoreChange)
93 }
94 }),
95 [mutationCache],
96 ),
97 () => result.current,
98 () => result.current,
99 )!
100}

Callers 6

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

Calls 5

useQueryClientFunction · 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