Open History to see the last commit
1---
2id: no-rest-destructuring
3title: Disallow object rest destructuring on query results
4---
5β
6Use object rest destructuring on query results automatically subscribes to every field of the query result, which may cause unnecessary re-renders.
7This makes sure that you only subscribe to the fields that you actually need.
8β
9## Rule Details
10β
11Examples of **incorrect** code for this rule:
12β
13```tsx
14/* eslint "@tanstack/query/no-rest-destructuring": "warn" */
15β
16const useTodos = () => {
17 const { data: todos, ...rest } = useQuery({
18 queryKey: ['todos'],
19 queryFn: () => api.getTodos(),
20 })
21 return { todos, ...rest }
22}
23```
24β
25Examples of **correct** code for this rule:
26β
27```tsx
28const todosQuery = useQuery({
29 queryKey: ['todos'],
30 queryFn: () => api.getTodos(),
31})
32β
33// normal object destructuring is fine
34const { data: todos } = todosQuery
35```
36β
37## When Not To Use It
38β
39If you set the `notifyOnChangeProps` options manually, you can disable this rule.
40Since you are not using tracked queries, you are responsible for specifying which props should trigger a re-render.
41β
42## Attributes
43β
44- [x] β
Recommended
45- [ ] π§ Fixable