Skip to content

docs: add docs for mutation-property-order eslint rule #9271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/eslint/eslint-plugin-query.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ Alternatively, add `@tanstack/query` to the plugins section, and configure the r
- [@tanstack/query/no-unstable-deps](../no-unstable-deps.md)
- [@tanstack/query/infinite-query-property-order](../infinite-query-property-order.md)
- [@tanstack/query/no-void-query-fn](../no-void-query-fn.md)
- [@tanstack/query/mutation-property-order](../mutation-property-order.md)
73 changes: 73 additions & 0 deletions docs/eslint/mutation-property-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
---
id: mutation-property-order
title: Ensure correct order of inference-sensitive properties in useMutation()
---

For the following functions, the property order of the passed in object matters due to type inference:

- `useMutation()`

The correct property order is as follows:

- `onMutate`
- `onError`
- `onSettled`

All other properties are insensitive to the order as they do not depend on type inference.

## Rule Details

Examples of **incorrect** code for this rule:

```tsx
/* eslint "@tanstack/query/mutation-property-order": "warn" */
import { useMutation } from '@tanstack/react-query'

const mutation = useMutation({
mutationFn: () => Promise.resolve('success'),
onSettled: () => {
results.push('onSettled-promise')
return Promise.resolve('also-ignored') // Promise<string> (should be ignored)
},
onMutate: async () => {
results.push('onMutate-async')
await sleep(1)
return { backup: 'async-data' }
},
onError: async () => {
results.push('onError-async-start')
await sleep(1)
results.push('onError-async-end')
},
})
```

Examples of **correct** code for this rule:

```tsx
/* eslint "@tanstack/query/mutation-property-order": "warn" */
import { useInfiniteQuery } from '@tanstack/react-query'

const mutation = useMutation({
mutationFn: () => Promise.resolve('success'),
onMutate: async () => {
results.push('onMutate-async')
await sleep(1)
return { backup: 'async-data' }
},
onError: async () => {
results.push('onError-async-start')
await sleep(1)
results.push('onError-async-end')
},
onSettled: () => {
results.push('onSettled-promise')
return Promise.resolve('also-ignored') // Promise<string> (should be ignored)
},
})
```

## Attributes

- [x] ✅ Recommended
- [x] 🔧 Fixable