Open History to see the last commit
1---
2id: MutationObserver
3title: MutationObserver
4---
5β
6Defined in: [packages/query-core/src/mutationObserver.ts:38](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L38)
7β
8Observes a single mutation and derives a `MutationObserverResult` from it.
9A framework hook like `useMutation` creates one `MutationObserver` per hook
10call, keeps it stable across re-renders, calls `setOptions` when the options
11passed to the hook change, subscribes to it to re-render on updates, and
12reads `getCurrentResult()` for the value to return. Calling `mutate()`
13builds a new underlying `Mutation` in the `MutationCache` and executes it.
14β
15## Example
16β
17```ts
18const observer = new MutationObserver(queryClient, {
19 mutationFn: (variables: { title: string }) => addPost(variables),
20})
21```
22β
23## Extends
24β
25- `Subscribable`\<`MutationObserverListener`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>\>
26β
27## Type Parameters
28β
29### TData
30β
31`TData` = `unknown`
32β
33### TError
34β
35`TError` = [`DefaultError`](../type-aliases/DefaultError.md)
36β
37### TVariables
38β
39`TVariables` = `void`
40β
41### TOnMutateResult
42β
43`TOnMutateResult` = `unknown`
44β
45## Constructors
46β
47### Constructor
48β
49```ts
50new MutationObserver<TData, TError, TVariables, TOnMutateResult>(client: QueryClient, options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>): MutationObserver<TData, TError, TVariables, TOnMutateResult>;
51```
52β
53Defined in: [packages/query-core/src/mutationObserver.ts:58](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L58)
54β
55#### Parameters
56β
57##### client
58β
59[`QueryClient`](QueryClient.md)
60β
61##### options
62β
63[`MutationObserverOptions`](../interfaces/MutationObserverOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
64β
65#### Returns
66β
67`MutationObserver`\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
68β
69#### Overrides
70β
71```ts
72Subscribable<
73 MutationObserverListener<TData, TError, TVariables, TOnMutateResult>
74>.constructor
75```
76β
77## Properties
78β
79### listeners
80β
81```ts
82protected listeners: Set<MutationObserverListener<TData, TError, TVariables, TOnMutateResult>>;
83```
84β
85Defined in: [packages/query-core/src/subscribable.ts:7](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L7)
86β
87#### Inherited from
88β
89```ts
90Subscribable.listeners
91```
92β
93***
94β
95### options
96β
97```ts
98options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>;
99```
100β
101Defined in: [packages/query-core/src/mutationObserver.ts:46](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L46)
102β
103## Methods
104β
105### bindMethods()
106β
107```ts
108protected bindMethods(): void;
109```
110β
111Defined in: [packages/query-core/src/mutationObserver.ts:75](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L75)
112β
113#### Returns
114β
115`void`
116β
117***
118β
119### getCurrentResult()
120β
121```ts
122getCurrentResult(): MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;
123```
124β
125Defined in: [packages/query-core/src/mutationObserver.ts:155](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L155)
126β
127Returns the observer's current result, derived from the observed
128mutation's state (or the default, `idle` state if no mutation has been
129built yet, e.g. before the first `mutate()` call or after `reset()`).
130β
131#### Returns
132β
133[`MutationObserverResult`](../type-aliases/MutationObserverResult.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
134β
135***
136β
137### hasListeners()
138β
139```ts
140hasListeners(): boolean;
141```
142β
143Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
144β
145Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
146β
147#### Returns
148β
149`boolean`
150β
151#### Inherited from
152β
153```ts
154Subscribable.hasListeners
155```
156β
157***
158β
159### mutate()
160β
161```ts
162mutate(variables: TVariables, options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>): Promise<TData>;
163```
164β
165Defined in: [packages/query-core/src/mutationObserver.ts:207](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L207)
166β
167Builds a new `Mutation` in the `MutationCache` using the observer's
168current options, detaches this observer from any previously observed
169mutation, attaches it to the new one, and executes it with the given
170variables.
171β
172The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
173invoked once the mutation settles, in addition to any callbacks defined
174on the observer's own options.
175β
176#### Parameters
177β
178##### variables
179β
180`TVariables`
181β
182##### options?
183β
184[`MutateOptions`](../interfaces/MutateOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
185β
186#### Returns
187β
188`Promise`\<`TData`\>
189β
190#### Example
191β
192```ts
193await observer.mutate(
194 { title: 'New post' },
195 { onSuccess: (data) => console.log(data) },
196)
197```
198β
199***
200β
201### onSubscribe()
202β
203```ts
204protected onSubscribe(): void;
205```
206β
207Defined in: [packages/query-core/src/mutationObserver.ts:127](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L127)
208β
209#### Returns
210β
211`void`
212β
213#### Overrides
214β
215```ts
216Subscribable.onSubscribe
217```
218β
219***
220β
221### onUnsubscribe()
222β
223```ts
224protected onUnsubscribe(): void;
225```
226β
227Defined in: [packages/query-core/src/mutationObserver.ts:135](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L135)
228β
229#### Returns
230β
231`void`
232β
233#### Overrides
234β
235```ts
236Subscribable.onUnsubscribe
237```
238β
239***
240β
241### reset()
242β
243```ts
244reset(): void;
245```
246β
247Defined in: [packages/query-core/src/mutationObserver.ts:180](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L180)
248β
249Detaches the observer from the mutation it is currently observing (if
250any) and resets the observed result back to its default, `idle` state.
251β
252This does not cancel an in-flight mutation; the mutation itself keeps
253running to completion and its own callbacks still fire, but this
254observer stops reflecting its state and a subsequent `mutate()` call
255will build a brand new mutation.
256β
257#### Returns
258β
259`void`
260β
261#### Example
262β
263```ts
264observer.reset()
265```
266β
267#### See
268β
269[MutationObserver#mutate](#mutate)
270β
271***
272β
273### setOptions()
274β
275```ts
276setOptions(options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>): void;
277```
278β
279Defined in: [packages/query-core/src/mutationObserver.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L96)
280β
281Updates the observer's options.
282β
283If the new `mutationKey` differs from the previous one (and both were
284defined), the observer is reset, detaching it from the mutation it was
285observing. Otherwise, if the currently observed mutation is still
286`pending`, its options are updated in place as well.
287β
288#### Parameters
289β
290##### options
291β
292[`MutationObserverOptions`](../interfaces/MutationObserverOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
293β
294#### Returns
295β
296`void`
297β
298#### Example
299β
300```ts
301observer.setOptions({
302 mutationFn: (variables: { title: string }) => addPost(variables),
303 onSuccess: (data) => console.log(data),
304})
305```
306β
307***
308β
309### subscribe()
310β
311```ts
312subscribe(listener: MutationObserverListener): () => void;
313```
314β
315Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
316β
317Registers a listener to be called on every update this object notifies about. Returns a function
318that removes the listener again β call it to stop listening. The base class never drops a listener
319on its own, though some subclasses clear all of theirs in `destroy()`.
320β
321#### Parameters
322β
323##### listener
324β
325`MutationObserverListener`
326β
327Called on each update, with whatever the subclass passes to its subscribers.
328β
329#### Returns
330β
331```ts
332(): void;
333```
334β
335##### Returns
336β
337`void`
338β
339#### Example
340β
341```ts
342const unsubscribe = subscribable.subscribe(() => {
343 // react to the update
344})
345β
346unsubscribe()
347```
348β
349#### Inherited from
350β
351```ts
352Subscribable.subscribe
353```