Skip to content

useTanstackQueryExhaustiveDependencies (JavaScript)

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

Require query keys to include every value that the query function uses.

TanStack Query saves the data returned by a query function under the query key, and fetches the data again when the key changes. When the query function uses a value that the key doesn’t include, changing that value doesn’t fetch new data. The query keeps returning the data that belongs to the old value, and queries that use different values share the same saved data.

This rule checks objects that have both a queryKey and a queryFn property, such as the options passed to useQuery or queryOptions. It reports the variables that the query function reads but the query key doesn’t include.

The rule only checks variables declared inside the function that contains the query options, such as a component, a hook, or a function that creates query options. It doesn’t report:

  • variables declared outside that function, such as imports and top-level constants;
  • functions that the query function calls, such as fetchTodo in fetchTodo(id);
  • objects whose methods the query function calls, such as api in api.getTodo(id);
  • classes used with new or instanceof.

When a property of a variable is in the query key, such as todo.id, the query function can read that property. When the whole variable is in the query key, the query function can read any of its properties.

In Vue files, the rule also checks variables declared at the top level of the script, except imports.

When queryFn chooses between a function and skipToken, such as enabled ? () => fetchTodo(id) : skipToken, the rule only checks the function.

import { useQuery } from "@tanstack/react-query";
function Todo({ todoId }) {
const query = useQuery({
queryKey: ["todo"],
queryFn: () => fetchTodo(todoId),
});
}
code-block.js:5:9 lint/nursery/useTanstackQueryExhaustiveDependencies  FIXABLE  ━━━━━━━━━━━━━━━━━━━━

✖ This query key doesn't include all the values that the query function uses.

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

ℹ The query function uses todoId here, but the query key doesn't include it.

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

ℹ TanStack Query saves the result of a query under its key, and fetches the data again when the key changes. When one of these values changes, the query doesn't fetch new data and keeps returning the data that belongs to the old value.

ℹ Add the missing values to the query key.

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

ℹ Unsafe fix: Add the missing values to the query key.

5 │ ········queryKey:·["todo",·todoId],
│ ++++++++
const todoQueries = {
detail: (id) => ({ queryKey: ["todo"], queryFn: () => fetchTodo(id) }),
};
code-block.js:2:24 lint/nursery/useTanstackQueryExhaustiveDependencies  FIXABLE  ━━━━━━━━━━━━━━━━━━━

✖ This query key doesn't include all the values that the query function uses.

1 │ const todoQueries = {
> 2 │ detail: (id) => ({ queryKey: ["todo"], queryFn: () => fetchTodo(id) }),
│ ^^^^^^^^^^^^^^^^^^
3 │ };
4 │

ℹ The query function uses id here, but the query key doesn't include it.

1 │ const todoQueries = {
> 2 │ detail: (id) => ({ queryKey: ["todo"], queryFn: () => fetchTodo(id) }),
│ ^^
3 │ };
4 │

ℹ TanStack Query saves the result of a query under its key, and fetches the data again when the key changes. When one of these values changes, the query doesn't fetch new data and keeps returning the data that belongs to the old value.

ℹ Add the missing values to the query key.

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

ℹ Unsafe fix: Add the missing values to the query key.

2 │ ····detail:·(id)·=>·({·queryKey:·["todo",·id],·queryFn:·()·=>·fetchTodo(id)·}),
│ ++++
import { useQuery } from "@tanstack/react-query";
function Todo({ todoId }) {
const todos = useTodos();
const query = useQuery({
queryKey: ["todo", todoId],
queryFn: () => todos.getTodo(todoId),
});
}
const todoQueries = {
detail: (id) => ({ queryKey: ["todo", id], queryFn: () => fetchTodo(id) }),
};

Names of variables that the query function can use without adding them to the query key. The query function can also read any property of these variables.

Default: []

biome.json
{
"linter": {
"rules": {
"nursery": {
"useTanstackQueryExhaustiveDependencies": {
"level": "on",
"options": {
"ignoredVariables": [
"config"
]
}
}
}
}
}
}
function useTodo(todoId, config) {
return useQuery({
queryKey: ["todo", todoId],
queryFn: () => fetchTodo(todoId, config.baseUrl),
});
}

Names of TypeScript types. The query function can use a variable or parameter declared with one of these types without adding it to the query key. The rule reads the type written after the name of the variable or parameter, such as config: Config. It also finds the type inside unions, intersections, arrays, and tuples, such as config: Config | undefined.

Default: []

biome.json
{
"linter": {
"rules": {
"nursery": {
"useTanstackQueryExhaustiveDependencies": {
"level": "on",
"options": {
"ignoredTypes": [
"Config"
]
}
}
}
}
}
}
function useTodo(todoId: string, config: Config) {
return useQuery({
queryKey: ["todo", todoId],
queryFn: () => fetchTodo(todoId, config.baseUrl),
});
}