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

prefer-query-object-syntax.md

62 lines1.2 KB

1---
2id: prefer-query-object-syntax
3title: Prefer object syntax for useQuery
4---
5​
6You can use [`useQuery`](https://tanstack.com/query/v4/docs/reference/useQuery) in two different ways.
7​
8Standard
9​
10```tsx
11useQuery(queryKey, queryFn?, options?)
12​
13// or
14​
15useQuery(options)
16```
17​
18This rule prefers the second option, as it is more consistent with other React Query hooks, like `useQueries`. It will also be the only available option in a future major version.
19​
20## Rule Details
21​
22Examples of **incorrect** code for this rule:
23​
24```js
25/* eslint "@tanstack/query/prefer-query-object-syntax": "error" */
26​
27import { useQuery } from '@tanstack/react-query';
28​
29useQuery(queryKey, queryFn, {
30 onSuccess,
31});
32​
33useQuery(queryKey, {
34 queryFn,
35 onSuccess,
36});
37```
38​
39Examples of **correct** code for this rule:
40​
41```js
42import { useQuery } from '@tanstack/react-query';
43​
44useQuery({
45 queryKey,
46 queryFn,
47 onSuccess,
48});
49```
50​
51## When Not To Use It
52​
53If you don't care about useQuery consistency, then you will not need this rule.
54​
55## Attributes
56​
57- [x] βœ… Recommended
58- [x] πŸ”§ Fixable
59​
60## Credits
61​
62This rule was initially developed by [KubaJastrz](https://github.com/KubaJastrz) in [eslint-plugin-react-query](https://github.com/KubaJastrz/eslint-plugin-react-query).