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### options
80β
81```ts
82options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>;
83```
84β
85Defined in: [packages/query-core/src/mutationObserver.ts:46](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L46)
86β
87## Methods
88β
89### getCurrentResult()
90β
91```ts
92getCurrentResult(): MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;
93```
94β
95Defined in: [packages/query-core/src/mutationObserver.ts:155](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L155)
96β
97Returns the observer's current result, derived from the observed
98mutation's state (or the default, `idle` state if no mutation has been
99built yet, e.g. before the first `mutate()` call or after `reset()`).
100β
101#### Returns
102β
103[`MutationObserverResult`](../type-aliases/MutationObserverResult.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
104β
105***
106β
107### hasListeners()
108β
109```ts
110hasListeners(): boolean;
111```
112β
113Defined in: [packages/query-core/src/subscribable.ts:41](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L41)
114β
115Returns `true` while at least one listener is registered, `false` once they have all unsubscribed.
116β
117#### Returns
118β
119`boolean`
120β
121#### Inherited from
122β
123```ts
124Subscribable.hasListeners
125```
126β
127***
128β
129### mutate()
130β
131```ts
132mutate(variables: TVariables, options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>): Promise<TData>;
133```
134β
135Defined in: [packages/query-core/src/mutationObserver.ts:207](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L207)
136β
137Builds a new `Mutation` in the `MutationCache` using the observer's
138current options, detaches this observer from any previously observed
139mutation, attaches it to the new one, and executes it with the given
140variables.
141β
142The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
143invoked once the mutation settles, in addition to any callbacks defined
144on the observer's own options.
145β
146#### Parameters
147β
148##### variables
149β
150`TVariables`
151β
152##### options?
153β
154[`MutateOptions`](../interfaces/MutateOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
155β
156#### Returns
157β
158`Promise`\<`TData`\>
159β
160#### Example
161β
162```ts
163await observer.mutate(
164 { title: 'New post' },
165 { onSuccess: (data) => console.log(data) },
166)
167```
168β
169***
170β
171### reset()
172β
173```ts
174reset(): void;
175```
176β
177Defined in: [packages/query-core/src/mutationObserver.ts:180](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L180)
178β
179Detaches the observer from the mutation it is currently observing (if
180any) and resets the observed result back to its default, `idle` state.
181β
182This does not cancel an in-flight mutation; the mutation itself keeps
183running to completion and its own callbacks still fire, but this
184observer stops reflecting its state and a subsequent `mutate()` call
185will build a brand new mutation.
186β
187#### Returns
188β
189`void`
190β
191#### Example
192β
193```ts
194observer.reset()
195```
196β
197#### See
198β
199[MutationObserver#mutate](#mutate)
200β
201***
202β
203### setOptions()
204β
205```ts
206setOptions(options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>): void;
207```
208β
209Defined in: [packages/query-core/src/mutationObserver.ts:96](https://github.com/TanStack/query/blob/main/packages/query-core/src/mutationObserver.ts#L96)
210β
211Updates the observer's options.
212β
213If the new `mutationKey` differs from the previous one (and both were
214defined), the observer is reset, detaching it from the mutation it was
215observing. Otherwise, if the currently observed mutation is still
216`pending`, its options are updated in place as well.
217β
218#### Parameters
219β
220##### options
221β
222[`MutationObserverOptions`](../interfaces/MutationObserverOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
223β
224#### Returns
225β
226`void`
227β
228#### Example
229β
230```ts
231observer.setOptions({
232 mutationFn: (variables: { title: string }) => addPost(variables),
233 onSuccess: (data) => console.log(data),
234})
235```
236β
237***
238β
239### subscribe()
240β
241```ts
242subscribe(listener: MutationObserverListener): () => void;
243```
244β
245Defined in: [packages/query-core/src/subscribable.ts:27](https://github.com/TanStack/query/blob/main/packages/query-core/src/subscribable.ts#L27)
246β
247Registers a listener to be called on every update this object notifies about. Returns a function
248that removes the listener again β call it to stop listening. The base class never drops a listener
249on its own, though some subclasses clear all of theirs in `destroy()`.
250β
251#### Parameters
252β
253##### listener
254β
255`MutationObserverListener`
256β
257Called on each update, with whatever the subclass passes to its subscribers.
258β
259#### Returns
260β
261```ts
262(): void;
263```
264β
265##### Returns
266β
267`void`
268β
269#### Example
270β
271```ts
272const unsubscribe = subscribable.subscribe(() => {
273 // react to the update
274})
275β
276unsubscribe()
277```
278β
279#### Inherited from
280β
281```ts
282Subscribable.subscribe
283```