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

no-rest-destructuring.md

47 lines1.3 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​
37When [typed linting](https://typescript-eslint.io/getting-started/typed-linting/) is enabled, the rule also flags rest destructuring on custom hooks that return a TanStack Query result.
38​
39## When Not To Use It
40​
41If you set the `notifyOnChangeProps` options manually, you can disable this rule.
42Since you are not using tracked queries, you are responsible for specifying which props should trigger a re-render.
43​
44## Attributes
45​
46- [x] βœ… Recommended
47- [ ] πŸ”§ Fixable