MCPcopy
hub / github.com/TanStack/query / useMutation

Function useMutation

packages/vue-query/src/useMutation.ts:74–149  ·  view source on GitHub ↗
(
  mutationOptions: MaybeRefDeep<
    UseMutationOptionsBase<TData, TError, TVariables, TOnMutateResult>
  >,
  queryClient?: QueryClient,
)

Source from the content-addressed store, hash-verified

72}
73
74export function useMutation<
75 TData = unknown,
76 TError = DefaultError,
77 TVariables = void,
78 TOnMutateResult = unknown,
79>(
80 mutationOptions: MaybeRefDeep<
81 UseMutationOptionsBase<TData, TError, TVariables, TOnMutateResult>
82 >,
83 queryClient?: QueryClient,
84): UseMutationReturnType<TData, TError, TVariables, TOnMutateResult> {
85 if (process.env.NODE_ENV === 'development') {
86 if (!getCurrentScope()) {
87 console.warn(
88 'vue-query composable like "useQuery()" should only be used inside a "setup()" function or a running effect scope. They might otherwise lead to memory leaks.',
89 )
90 }
91 }
92
93 const client = queryClient || useQueryClient()
94 const options = computed(() => {
95 return client.defaultMutationOptions(cloneDeepUnref(mutationOptions))
96 })
97 const observer = new MutationObserver(client, options.value)
98 const state = options.value.shallow
99 ? shallowReactive(observer.getCurrentResult())
100 : reactive(observer.getCurrentResult())
101
102 const unsubscribe = observer.subscribe((result) => {
103 updateState(state, result)
104 })
105
106 const mutate = (
107 variables: TVariables,
108 mutateOptions?: MutateOptions<TData, TError, TVariables, TOnMutateResult>,
109 ) => {
110 observer.mutate(variables, mutateOptions).catch(() => {
111 // This is intentional
112 })
113 }
114
115 watch(options, () => {
116 observer.setOptions(options.value)
117 })
118
119 onScopeDispose(() => {
120 unsubscribe()
121 })
122
123 const readonlyState = options.value.shallow
124 ? shallowReadonly(state)
125 : readonly(state)
126
127 const resultRefs = toRefs(readonlyState) as ToRefs<
128 Readonly<MutationResult<TData, TError, TVariables, TOnMutateResult>>
129 >
130
131 watch(

Callers 7

useDeleteSomethingFunction · 0.50
useDeleteSomethingFunction · 0.50

Calls 9

getCurrentResultMethod · 0.95
setOptionsMethod · 0.95
useQueryClientFunction · 0.90
cloneDeepUnrefFunction · 0.90
updateStateFunction · 0.90
shouldThrowErrorFunction · 0.90
unsubscribeFunction · 0.70
subscribeMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…