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 a query’s data under its 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, and the query keeps returning the old data.
This rule checks objects that have both a queryKey and a queryFn property, such as
the options passed to useQuery or queryOptions. It only reports variables declared
inside the function that contains those options, such as a component or a hook. It
doesn’t report functions that the query function calls, or objects whose methods it
calls, such as api in api.getTodo(id).
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”© 2023-present Biome developers and contributors.