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

no-rest-destructuring.md

45 lines1.1 KB

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 }
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