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

useMutation.md

97 lines4.4 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<void> | 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<void> | 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<void> | 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: (retryAttempt: number) => number`
66 - This function receives a `retryAttempt` integer 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`
70 - Defaults to the global query config's `useErrorBoundary` value, which is `false`
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​
73**Returns**
74​
75- `mutate: (variables: TVariables, { onSuccess, onSettled, onError }) => void`
76 - The mutation function you can call with variables to trigger the mutation and optionally override options passed to `useMutation`.
77 - `variables: TVariables`
78 - Optional
79 - The variables object to pass to the `mutationFn`.
80 - Remaining options extend the same options described above in the `useMutation` hook.
81 - If you make multiple requests, `onSuccess` will fire only after the latest call you've made.
82- `mutateAsync: (variables: TVariables, { onSuccess, onSettled, onError }) => Promise<TData>`
83 - Similar to `mutate` but returns a promise which can be awaited.
84- `status: string`
85 - Will be:
86 - `idle` initial status prior to the mutation function executing.
87 - `loading` if the mutation is currently executing.
88 - `error` if the last mutation attempt resulted in an error.
89 - `success` if the last mutation attempt was successful.
90- `isIdle`, `isLoading`, `isSuccess`, `isError`: boolean variables derived from `status`
91- `data: undefined | unknown`
92 - Defaults to `undefined`
93 - The last successfully resolved data for the query.
94- `error: null | TError`
95 - The error object for the query, if an error was encountered.
96- `reset: () => void`
97 - A function to clean the mutation internal state (i.e., it resets the mutation to its initial state).