Skip to content

useTanstackQueryQueryOptions (JavaScript)

Language JavaScript (and super languages)
biome.json
{
"linter": {
"rules": {
"nursery": {
"useTanstackQueryQueryOptions": "error"
}
}
}
}

Require TanStack Query options to be created with queryOptions().

TanStack Query stores the data of a query under its key (queryKey). When the key and the function that fetches the data (queryFn) are written again in each place that uses the query, the same key can end up with two different functions. queryOptions() and infiniteQueryOptions() create the options once, so the two stay together.

This rule reports:

  • An object that sets queryKey or queryFn, written directly in a call to a query hook such as useQuery or useQueries, or to a query client method such as fetchQuery.
  • A query key written as an array, such as ["todos"], in a call to useIsFetching or to a query client method such as getQueryData or invalidateQueries.
import { useQuery } from "@tanstack/react-query";
function Todo({ id }) {
const query = useQuery({
queryKey: ["todo", id],
queryFn: () => fetchTodo(id),
});
}
code-block.js:4:28 lint/nursery/useTanstackQueryQueryOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The options of this query are written where the query is used.

3 │ function Todo({ id }) {
> 4 │ const query = useQuery({
│ ^
> 5 │ queryKey: ["todo", id],
> 6 │ queryFn: () => fetchTodo(id),
> 7 │ });
│ ^
8 │ }
9 │

ℹ When queryKey and queryFn are written again in each place that uses the query, the same key can end up with two different functions. One place then receives the data that the other one fetched.

ℹ Create the options once with queryOptions(), and pass the result here.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { useQueries } from "@tanstack/react-query";
function Dashboard() {
const results = useQueries({
queries: [{ queryKey: ["todos"], queryFn: fetchTodos }, userOptions],
});
}
code-block.js:5:19 lint/nursery/useTanstackQueryQueryOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The options of this query are written where the query is used.

3 │ function Dashboard() {
4 │ const results = useQueries({
> 5 │ queries: [{ queryKey: ["todos"], queryFn: fetchTodos }, userOptions],
│ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6 │ });
7 │ }

ℹ When queryKey and queryFn are written again in each place that uses the query, the same key can end up with two different functions. One place then receives the data that the other one fetched.

ℹ Create the options once with queryOptions(), and pass the result here.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { useQueryClient } from "@tanstack/react-query";
function useCachedTodo(id) {
const queryClient = useQueryClient();
return queryClient.getQueryData(["todo", id]);
}
code-block.js:5:37 lint/nursery/useTanstackQueryQueryOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This query key is written by hand.

3 │ function useCachedTodo(id) {
4 │ const queryClient = useQueryClient();
> 5 │ return queryClient.getQueryData(["todo", id]);
│ ^^^^^^^^^^^^
6 │ }
7 │

ℹ A key that is written again can differ from the key of the query, for example after one of them is changed. This call then no longer finds the query.

ℹ Create the options of the query with queryOptions(), and read the queryKey property of the result here.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { useQueryClient } from "@tanstack/react-query";
function useRefreshTodo(id) {
const queryClient = useQueryClient();
return () => queryClient.invalidateQueries({ queryKey: ["todo", id] });
}
code-block.js:5:60 lint/nursery/useTanstackQueryQueryOptions ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ This query key is written by hand.

3 │ function useRefreshTodo(id) {
4 │ const queryClient = useQueryClient();
> 5 │ return () => queryClient.invalidateQueries({ queryKey: ["todo", id] });
│ ^^^^^^^^^^^^
6 │ }
7 │

ℹ A key that is written again can differ from the key of the query, for example after one of them is changed. This call then no longer finds the query.

ℹ Create the options of the query with queryOptions(), and read the queryKey property of the result here.

ℹ This rule belongs to the nursery group, which means it is not yet stable and may change in the future. Visit https://biomejs.dev/linter/#nursery for more information.

import { queryOptions, useQuery, useQueryClient } from "@tanstack/react-query";
function todoOptions(id) {
return queryOptions({
queryKey: ["todo", id],
queryFn: () => fetchTodo(id),
});
}
function Todo({ id }) {
const query = useQuery(todoOptions(id));
const title = useQuery({ ...todoOptions(id), select: (todo) => todo.title });
}
function useRefreshTodo(id) {
const queryClient = useQueryClient();
const cached = queryClient.getQueryData(todoOptions(id).queryKey);
return () => queryClient.invalidateQueries({ queryKey: todoOptions(id).queryKey });
}