Open History to see the last commit
1---
2id: mutation-property-order
3title: Ensure correct order of inference-sensitive properties in useMutation()
4---
5β
6For the following functions, the property order of the passed in object matters due to type inference:
7β
8- `useMutation()`
9β
10The correct property order is as follows:
11β
12- `onMutate`
13- `onError`
14- `onSettled`
15β
16All other properties are insensitive to the order as they do not depend on type inference.
17β
18## Rule Details
19β
20Examples of **incorrect** code for this rule:
21β
22```tsx
23/* eslint "@tanstack/query/mutation-property-order": "warn" */
24import { useMutation } from '@tanstack/react-query'
25β
26const mutation = useMutation({
27 mutationFn: () => Promise.resolve('success'),
28 onSettled: () => {
29 results.push('onSettled-promise')
30 return Promise.resolve('also-ignored') // Promise<string> (should be ignored)
31 },
32 onMutate: async () => {
33 results.push('onMutate-async')
34 await sleep(1)
35 return { backup: 'async-data' }
36 },
37 onError: async () => {
38 results.push('onError-async-start')
39 await sleep(1)
40 results.push('onError-async-end')
41 },
42})
43```
44β
45Examples of **correct** code for this rule:
46β
47```tsx
48/* eslint "@tanstack/query/mutation-property-order": "warn" */
49import { useMutation } from '@tanstack/react-query'
50β
51const mutation = useMutation({
52 mutationFn: () => Promise.resolve('success'),
53 onMutate: async () => {
54 results.push('onMutate-async')
55 await sleep(1)
56 return { backup: 'async-data' }
57 },
58 onError: async () => {
59 results.push('onError-async-start')
60 await sleep(1)
61 results.push('onError-async-end')
62 },
63 onSettled: () => {
64 results.push('onSettled-promise')
65 return Promise.resolve('also-ignored') // Promise<string> (should be ignored)
66 },
67})
68```
69β
70## Attributes
71β
72- [x] β
Recommended
73- [x] π§ Fixable