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

Function useMutationState

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

Source from the content-addressed store, hash-verified

135 * ```
136 */
137export function useMutationState<
138 TResult = MutationState,
139 TMutation extends Mutation<any, any, any, any> =
140 MutationTypeFromResult<TResult>,
141>(
142 options: MutationStateOptions<TResult, TMutation> = {},
143 queryClient?: QueryClient,
144): Array<TResult> {
145 const mutationCache = useQueryClient(queryClient).getMutationCache()
146 const optionsRef = useRef(options)
147 const result = useRef<Array<TResult>>(null)
148 if (result.current === null) {
149 result.current = getResult(mutationCache, options)
150 }
151
152 useEffect(() => {
153 optionsRef.current = options
154 })
155
156 return useSyncExternalStore(
157 useCallback(
158 (onStoreChange) =>
159 mutationCache.subscribe(() => {
160 const nextResult = replaceEqualDeep(
161 result.current,
162 getResult(mutationCache, optionsRef.current),
163 )
164 if (result.current !== nextResult) {
165 result.current = nextResult
166 notifyManager.schedule(onStoreChange)
167 }
168 }),
169 [mutationCache],
170 ),
171 () => result.current,
172 )!
173}

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