Open History to see the last commit
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 // ...
21}
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 })
32}
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 })
45}
46β
47function Component({ id }) {
48 const query = useQuery(getFooOptions(id))
49 // ...
50}
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 })
61}
62β
63function useFooQuery(id) {
64 return useQuery({ ...getFooOptions(id), select: (data) => data.foo })
65}
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 })
80}
81β
82function Component({ id }) {
83 const queryClient = useQueryClient()
84 return queryClient.getQueryData(['todo', id])
85}
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 })
96}
97β
98function Component({ id }) {
99 const queryClient = useQueryClient()
100 return queryClient.invalidateQueries({ queryKey: ['todo', id] })
101}
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 })
114}
115β
116function Component({ id }) {
117 const queryClient = useQueryClient()
118 return queryClient.getQueryData(todoOptions(id).queryKey)
119}
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 })
130}
131β
132function Component({ id }) {
133 const queryClient = useQueryClient()
134 return queryClient.invalidateQueries({ queryKey: todoOptions(id).queryKey })
135}
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