Skip to content

useTanstackQueryMutationPropertyOrder (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useTanstackQueryMutationPropertyOrder": "error"
}
}
}
}

Enforce that onMutate comes before onError and onSettled in the options of TanStack Query mutations.

TanStack Query passes the value that onMutate returns to onError and onSettled. TypeScript works out the type of that value from the properties of the options object, in the order in which they are written. When onMutate comes after onError or onSettled, the type of the value in that function can be wrong. The other properties can be in any position, and onError and onSettled can be in either order.

This rule checks the options object passed to useMutation, when useMutation is imported from a TanStack Query package such as @tanstack/react-query.

The code action moves onMutate in front of the first onError or onSettled property, and keeps the other properties in the same order. It is unsafe because it changes the order in which the property values are evaluated. When a spread such as ...sharedOptions is written between the two properties, the code action can also change which of the spread and onMutate overrides the other. The code action isn’t offered when it would move a property onto the same line as a comment that comes before it.

import { useMutation } from "@tanstack/react-query";
useMutation({
mutationFn: (todo) => saveTodo(todo),
onError: (error, todo, saved) => restoreTodo(saved.previousTodo),
onMutate: (todo) => ({ previousTodo: readTodo(todo.id) }),
});
code-block.js:6:5 lint/nursery/useTanstackQueryMutationPropertyOrder  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━

⚠ This onMutate property comes after onError.

4 │ mutationFn: (todo) => saveTodo(todo),
5 │ onError: (error, todo, saved) => restoreTodo(saved.previousTodo),
> 6 │ onMutate: (todo) => ({ previousTodo: readTodo(todo.id) }),
│ ^^^^^^^^
7 │ });
8 │

ℹ TanStack Query passes the value that onMutate returns to onError and onSettled. The type of that value is worked out from the properties in the order in which they are written, so it can be wrong in a property that comes before onMutate.

ℹ Move onMutate before onError and onSettled.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move onMutate before onError.

3 3 │ useMutation({
4 4 │ mutationFn: (todo) => saveTodo(todo),
5 │ - ····onError:·(error,·todo,·saved)·=>·restoreTodo(saved.previousTodo),
6 │ - ····onMutate:·(todo)·=>·({·previousTodo:·readTodo(todo.id)·}),
5 │ + ····onMutate:·(todo)·=>·({·previousTodo:·readTodo(todo.id)·}),
6 │ + ····onError:·(error,·todo,·saved)·=>·restoreTodo(saved.previousTodo),
7 7 │ });
8 8 │

import { useMutation } from "@tanstack/react-query";
useMutation({
onSettled: (data, error, todo, saved) => refreshTodo(saved.previousTodo),
mutationFn: (todo) => saveTodo(todo),
onMutate: (todo) => ({ previousTodo: readTodo(todo.id) }),
});
code-block.js:6:5 lint/nursery/useTanstackQueryMutationPropertyOrder  FIXABLE  ━━━━━━━━━━━━━━━━━━━━━

⚠ This onMutate property comes after onSettled.

4 │ onSettled: (data, error, todo, saved) => refreshTodo(saved.previousTodo),
5 │ mutationFn: (todo) => saveTodo(todo),
> 6 │ onMutate: (todo) => ({ previousTodo: readTodo(todo.id) }),
│ ^^^^^^^^
7 │ });
8 │

ℹ TanStack Query passes the value that onMutate returns to onError and onSettled. The type of that value is worked out from the properties in the order in which they are written, so it can be wrong in a property that comes before onMutate.

ℹ Move onMutate before onError and onSettled.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

ℹ Unsafe fix: Move onMutate before onSettled.

2 2 │
3 3 │ useMutation({
4 │ - ····onSettled:·(data,·error,·todo,·saved)·=>·refreshTodo(saved.previousTodo),
5 │ - ····mutationFn:·(todo)·=>·saveTodo(todo),
6 │ - ····onMutate:·(todo)·=>·({·previousTodo:·readTodo(todo.id)·}),
4 │ + ····onMutate:·(todo)·=>·({·previousTodo:·readTodo(todo.id)·}),
5 │ + ····onSettled:·(data,·error,·todo,·saved)·=>·refreshTodo(saved.previousTodo),
6 │ + ····mutationFn:·(todo)·=>·saveTodo(todo),
7 7 │ });
8 8 │

import { useMutation } from "@tanstack/react-query";
useMutation({
mutationFn: (todo) => saveTodo(todo),
onMutate: (todo) => ({ previousTodo: readTodo(todo.id) }),
onError: (error, todo, saved) => restoreTodo(saved.previousTodo),
onSettled: (data, error, todo, saved) => refreshTodo(saved.previousTodo),
});
useMutation({
onMutate: (todo) => ({ previousTodo: readTodo(todo.id) }),
onSettled: (data, error, todo, saved) => refreshTodo(saved.previousTodo),
onError: (error, todo, saved) => restoreTodo(saved.previousTodo),
mutationFn: (todo) => saveTodo(todo),
});