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

useMutation.md

99 lines4.8 KB

1---
2id: useMutation
3title: useMutation
4---
5​
6```js
7const {
8 data,
9 error,
10 isError,
11 isIdle,
12 isLoading,
13 isPaused,
14 isSuccess,
15 mutate,
16 mutateAsync,
17 reset,
18 status,
19} = useMutation(mutationFn, {
20 mutationKey,
21 onError,
22 onMutate,
23 onSettled,
24 onSuccess,
25 useErrorBoundary,
26})
27​
28mutate(variables, {
29 onError,
30 onSettled,
31 onSuccess,
32})
33```
34​
35**Options**
36​
37- `mutationFn: (variables: TVariables) => Promise<TData>`
38 - **Required**
39 - A function that performs an asynchronous task and returns a promise.
40 - `variables` is an object that `mutate` will pass to your `mutationFn`
41- `mutationKey: string`
42 - Optional
43 - A mutation key can be set to inherit defaults set with `queryClient.setMutationDefaults` or to identify the mutation in the devtools.
44- `onMutate: (variables: TVariables) => Promise<TContext | void> | TContext | void`
45 - Optional
46 - This function will fire before the mutation function is fired and is passed the same variables the mutation function would receive
47 - Useful to perform optimistic updates to a resource in hopes that the mutation succeeds
48 - The value returned from this function will be passed to both the `onError` and `onSettled` functions in the event of a mutation failure and can be useful for rolling back optimistic updates.
49- `onSuccess: (data: TData, variables: TVariables, context?: TContext) => Promise<unknown> | void`
50 - Optional
51 - This function will fire when the mutation is successful and will be passed the mutation's result.
52 - If a promise is returned, it will be awaited and resolved before proceeding
53- `onError: (err: TError, variables: TVariables, context?: TContext) => Promise<unknown> | void`
54 - Optional
55 - This function will fire if the mutation encounters an error and will be passed the error.
56 - If a promise is returned, it will be awaited and resolved before proceeding
57- `onSettled: (data: TData, error: TError, variables: TVariables, context?: TContext) => Promise<unknown> | void`
58 - Optional
59 - This function will fire when the mutation is either successfully fetched or encounters an error and be passed either the data or error
60 - If a promise is returned, it will be awaited and resolved before proceeding
61- `retry: boolean | number | (failureCount: number, error: TError) => boolean`
62 - If `false`, failed mutations will not retry by default.
63 - If `true`, failed mutations will retry infinitely.
64 - If set to an `number`, e.g. `3`, failed mutations will retry until the failed mutations count meets that number.
65- `retryDelay: number | (retryAttempt: number, error: TError) => number`
66 - This function receives a `retryAttempt` integer and the actual Error and returns the delay to apply before the next attempt in milliseconds.
67 - A function like `attempt => Math.min(attempt > 1 ? 2 ** attempt * 1000 : 1000, 30 * 1000)` applies exponential backoff.
68 - A function like `attempt => attempt * 1000` applies linear backoff.
69- `useErrorBoundary: undefined | boolean | (error: TError) => boolean`
70 - Defaults to the global query config's `useErrorBoundary` value, which is `undefined`
71 - Set this to `true` if you want mutation errors to be thrown in the render phase and propagate to the nearest error boundary
72 - Set this to `false` to disable the behaviour of throwing errors to the error boundary.
73 - If set to a function, it will be passed the error and should return a boolean indicating whether to show the error in an error boundary (`true`) or return the error as state (`false`)
74​
75**Returns**
76​
77- `mutate: (variables: TVariables, { onSuccess, onSettled, onError }) => void`
78 - The mutation function you can call with variables to trigger the mutation and optionally override options passed to `useMutation`.
79 - `variables: TVariables`
80 - Optional
81 - The variables object to pass to the `mutationFn`.
82 - Remaining options extend the same options described above in the `useMutation` hook.
83 - If you make multiple requests, `onSuccess` will fire only after the latest call you've made.
84- `mutateAsync: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>`
85 - Similar to `mutate` but returns a promise which can be awaited.
86- `status: string`
87 - Will be:
88 - `idle` initial status prior to the mutation function executing.
89 - `loading` if the mutation is currently executing.
90 - `error` if the last mutation attempt resulted in an error.
91 - `success` if the last mutation attempt was successful.
92- `isIdle`, `isLoading`, `isSuccess`, `isError`: boolean variables derived from `status`
93- `data: undefined | unknown`
94 - Defaults to `undefined`
95 - The last successfully resolved data for the query.
96- `error: null | TError`
97 - The error object for the query, if an error was encountered.
98- `reset: () => void`
99 - A function to clean the mutation internal state (i.e., it resets the mutation to its initial state).