useTanstackQueryExhaustiveDependencies (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.9 - Diagnostic Category:
lint/nursery/useTanstackQueryExhaustiveDependencies - This rule has an unsafe fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Same as
@tanstack/query/exhaustive-deps
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "useTanstackQueryExhaustiveDependencies": "error" } } }}Description
Section titled “Description”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
fetchTodoinfetchTodo(id); - objects whose methods the query function calls, such as
apiinapi.getTodo(id); - classes used with
neworinstanceof.
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.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”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) }),};Options
Section titled “Options”ignoredVariables
Section titled “ignoredVariables”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: []
{ "linter": { "rules": { "nursery": { "useTanstackQueryExhaustiveDependencies": { "level": "on", "options": { "ignoredVariables": [ "config" ] } } } } }}function useTodo(todoId, config) { return useQuery({ queryKey: ["todo", todoId], queryFn: () => fetchTodo(todoId, config.baseUrl), });}ignoredTypes
Section titled “ignoredTypes”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: []
{ "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), });}Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.