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

mutation-property-order.md

73 lines1.7 KB

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