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

MutationObserver.md

283 lines6.6 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### 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) },
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```