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

MutationObserver.md

345 lines6.3 KB

1---
2id: MutationObserver
3title: MutationObserver
4---
5​
6Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:21
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/dist-ts/src/mutationObserver.d.ts:24
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<MutationObserverListener<TData, TError, TVariables, TOnMutateResult>>.constructor
73```
74​
75## Properties
76​
77### listeners
78​
79```ts
80protected listeners: Set<MutationObserverListener<TData, TError, TVariables, TOnMutateResult>>;
81```
82​
83Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:2
84​
85#### Inherited from
86​
87```ts
88Subscribable.listeners
89```
90​
91***
92​
93### options
94​
95```ts
96options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>;
97```
98​
99Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:23
100​
101## Methods
102​
103### bindMethods()
104​
105```ts
106protected bindMethods(): void;
107```
108​
109Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:25
110​
111#### Returns
112​
113`void`
114​
115***
116​
117### getCurrentResult()
118​
119```ts
120getCurrentResult(): MutationObserverResult<TData, TError, TVariables, TOnMutateResult>;
121```
122​
123Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:52
124​
125Returns the observer's current result, derived from the observed
126mutation's state (or the default, `idle` state if no mutation has been
127built yet, e.g. before the first `mutate()` call or after `reset()`).
128​
129#### Returns
130​
131[`MutationObserverResult`](../type-aliases/MutationObserverResult.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
132​
133***
134​
135### hasListeners()
136​
137```ts
138hasListeners(): boolean;
139```
140​
141Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:5
142​
143#### Returns
144​
145`boolean`
146​
147#### Inherited from
148​
149```ts
150Subscribable.hasListeners
151```
152​
153***
154​
155### mutate()
156​
157```ts
158mutate(variables: TVariables, options?: MutateOptions<TData, TError, TVariables, TOnMutateResult>): Promise<TData>;
159```
160​
161Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:88
162​
163Builds a new `Mutation` in the `MutationCache` using the observer's
164current options, detaches this observer from any previously observed
165mutation, attaches it to the new one, and executes it with the given
166variables.
167​
168The optional per-call `options` (`onSuccess`/`onError`/`onSettled`) are
169invoked once the mutation settles, in addition to any callbacks defined
170on the observer's own options.
171​
172#### Parameters
173​
174##### variables
175​
176`TVariables`
177​
178##### options?
179​
180[`MutateOptions`](../interfaces/MutateOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
181​
182#### Returns
183​
184`Promise`\<`TData`\>
185​
186#### Example
187​
188```ts
189await observer.mutate(
190 { title: 'New post' },
191 { onSuccess: (data) => console.log(data) },
193```
194​
195***
196​
197### onSubscribe()
198​
199```ts
200protected onSubscribe(): void;
201```
202​
203Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:43
204​
205#### Returns
206​
207`void`
208​
209#### Overrides
210​
211```ts
212Subscribable.onSubscribe
213```
214​
215***
216​
217### onUnsubscribe()
218​
219```ts
220protected onUnsubscribe(): void;
221```
222​
223Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:44
224​
225#### Returns
226​
227`void`
228​
229#### Overrides
230​
231```ts
232Subscribable.onUnsubscribe
233```
234​
235***
236​
237### reset()
238​
239```ts
240reset(): void;
241```
242​
243Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:69
244​
245Detaches the observer from the mutation it is currently observing (if
246any) and resets the observed result back to its default, `idle` state.
247​
248This does not cancel an in-flight mutation; the mutation itself keeps
249running to completion and its own callbacks still fire, but this
250observer stops reflecting its state and a subsequent `mutate()` call
251will build a brand new mutation.
252​
253#### Returns
254​
255`void`
256​
257#### Example
258​
259```ts
260observer.reset()
261```
262​
263#### See
264​
265[MutationObserver#mutate](#mutate)
266​
267***
268​
269### setOptions()
270​
271```ts
272setOptions(options: MutationObserverOptions<TData, TError, TVariables, TOnMutateResult>): void;
273```
274​
275Defined in: packages/query-core/dist-ts/src/mutationObserver.d.ts:42
276​
277Updates the observer's options.
278​
279If the new `mutationKey` differs from the previous one (and both were
280defined), the observer is reset, detaching it from the mutation it was
281observing. Otherwise, if the currently observed mutation is still
282`pending`, its options are updated in place as well.
283​
284#### Parameters
285​
286##### options
287​
288[`MutationObserverOptions`](../interfaces/MutationObserverOptions.md)\<`TData`, `TError`, `TVariables`, `TOnMutateResult`\>
289​
290#### Returns
291​
292`void`
293​
294#### Example
295​
296```ts
297observer.setOptions({
298 mutationFn: (variables: { title: string }) => addPost(variables),
299 onSuccess: (data) => console.log(data),
300})
301```
302​
303***
304​
305### subscribe()
306​
307```ts
308subscribe(listener: MutationObserverListener): () => void;
309```
310​
311Defined in: packages/query-core/dist-ts/src/subscribable.d.ts:4
312​
313#### Parameters
314​
315##### listener
316​
317`MutationObserverListener`
318​
319Called on each update, with whatever the subclass passes to its subscribers.
320​
321#### Returns
322​
323```ts
324(): void;
325```
326​
327##### Returns
328​
329`void`
330​
331#### Example
332​
333```ts
334const unsubscribe = subscribable.subscribe(() => {
335 // react to the update
336})
337​
338unsubscribe()
339```
340​
341#### Inherited from
342​
343```ts
344Subscribable.subscribe
345```