Framework
Version
Menu
Getting Started
Guides & Concepts
Community Resources
Examples
ESLint
Plugins
API Reference

Prefer object syntax for useQuery

You can use useQuery in two different ways.

Standard

tsx
useQuery(queryKey, queryFn?, options?)
// or
useQuery(options)

This 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.

Rule Details

Examples of incorrect code for this rule:

js
/* eslint "@tanstack/query/prefer-query-object-syntax": "error" */
import { useQuery } from '@tanstack/react-query';
useQuery(queryKey, queryFn, {
onSuccess,
});
useQuery(queryKey, {
queryFn,
onSuccess,
});

Examples of correct code for this rule:

js
import { useQuery } from '@tanstack/react-query';
useQuery({
queryKey,
queryFn,
onSuccess,
});

When Not To Use It

If you don't care about useQuery consistency, then you will not need this rule.

Attributes

  • ✅ Recommended
  • 🔧 Fixable

Credits

This rule was initially developed by KubaJastrz in eslint-plugin-react-query.

Want to Skip the Docs?

Fast track your learning and
take the offical React Query course ↗️

Subscribe to Bytes

Your weekly dose of JavaScript news. Delivered every Monday to over 100,000 devs, for free.

Bytes

No spam. Unsubscribe at any time.