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

prefer-query-options.md

145 lines3.2 KB

1---
2id: prefer-query-options
3title: Prefer the use of queryOptions
4---
5​
6Separating `queryKey` and `queryFn` can cause unexpected runtime issues when the same query key is accidentally used with more than one `queryFn`. Wrapping them in `queryOptions` (or `infiniteQueryOptions`) co-locates the key and function, making queries safer and easier to reuse.
7​
8## Rule Details
9​
10Examples of **incorrect** code for this rule:
11​
12```tsx
13/* eslint "@tanstack/query/prefer-query-options": "error" */
14​
15function Component({ id }) {
16 const query = useQuery({
17 queryKey: ['get', id],
18 queryFn: () => Api.get(`/foo/${id}`),
19 })
20 // ...
22```
23​
24```tsx
25/* eslint "@tanstack/query/prefer-query-options": "error" */
26​
27function useFooQuery(id) {
28 return useQuery({
29 queryKey: ['get', id],
30 queryFn: () => Api.get(`/foo/${id}`),
31 })
33```
34​
35Examples of **correct** code for this rule:
36​
37```tsx
38/* eslint "@tanstack/query/prefer-query-options": "error" */
39​
40function getFooOptions(id) {
41 return queryOptions({
42 queryKey: ['get', id],
43 queryFn: () => Api.get(`/foo/${id}`),
44 })
46​
47function Component({ id }) {
48 const query = useQuery(getFooOptions(id))
49 // ...
51```
52​
53```tsx
54/* eslint "@tanstack/query/prefer-query-options": "error" */
55​
56function getFooOptions(id) {
57 return queryOptions({
58 queryKey: ['get', id],
59 queryFn: () => Api.get(`/foo/${id}`),
60 })
62​
63function useFooQuery(id) {
64 return useQuery({ ...getFooOptions(id), select: (data) => data.foo })
66```
67​
68The rule also enforces reusing `queryKey` from a `queryOptions` result instead of typing it manually in `QueryClient` methods or filters.
69​
70Examples of **incorrect** `queryKey` references for this rule:
71​
72```tsx
73/* eslint "@tanstack/query/prefer-query-options": "error" */
74​
75function todoOptions(id) {
76 return queryOptions({
77 queryKey: ['todo', id],
78 queryFn: () => api.getTodo(id),
79 })
81​
82function Component({ id }) {
83 const queryClient = useQueryClient()
84 return queryClient.getQueryData(['todo', id])
86```
87​
88```tsx
89/* eslint "@tanstack/query/prefer-query-options": "error" */
90​
91function todoOptions(id) {
92 return queryOptions({
93 queryKey: ['todo', id],
94 queryFn: () => api.getTodo(id),
95 })
97​
98function Component({ id }) {
99 const queryClient = useQueryClient()
100 return queryClient.invalidateQueries({ queryKey: ['todo', id] })
102```
103​
104Examples of **correct** `queryKey` references for this rule:
105​
106```tsx
107/* eslint "@tanstack/query/prefer-query-options": "error" */
108​
109function todoOptions(id) {
110 return queryOptions({
111 queryKey: ['todo', id],
112 queryFn: () => api.getTodo(id),
113 })
115​
116function Component({ id }) {
117 const queryClient = useQueryClient()
118 return queryClient.getQueryData(todoOptions(id).queryKey)
120```
121​
122```tsx
123/* eslint "@tanstack/query/prefer-query-options": "error" */
124​
125function todoOptions(id) {
126 return queryOptions({
127 queryKey: ['todo', id],
128 queryFn: () => api.getTodo(id),
129 })
131​
132function Component({ id }) {
133 const queryClient = useQueryClient()
134 return queryClient.invalidateQueries({ queryKey: todoOptions(id).queryKey })
136```
137​
138## When Not To Use It
139​
140If you do not want to enforce the use of `queryOptions` in your codebase, you will not need this rule.
141​
142## Attributes
143​
144- [x] βœ… Recommended (strict)
145- [ ] πŸ”§ Fixable