Skip to content
Source
Sign inSign up
TanStack/query
Open History to see the last commit

MutationObserver.md

353 lines7.8 KB

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) },
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```