Skip to content

noTanstackQueryUnstableDeps (JavaScript)

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

Disallow using the result of a TanStack Query hook as a React hook dependency.

TanStack Query hooks such as useQuery and useMutation can return a new object on every render. When that object is a dependency of useEffect, useMemo or useCallback, the hook runs again on every render. List the values you need from the object instead.

useQueries and useSuspenseQueries are allowed when they are called with a combine option.

import { useCallback } from "react";
import { useMutation } from "@tanstack/react-query";
function Component() {
const mutation = useMutation({ mutationFn: (value) => value });
const callback = useCallback(() => {
mutation.mutate("hello");
}, [mutation]);
}
code-block.js:8:9 lint/nursery/noTanstackQueryUnstableDeps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The object returned by useMutation is a dependency of useCallback.

6 │ const callback = useCallback(() => {
7 │ mutation.mutate("hello");
> 8 │ }, [mutation]);
│ ^^^^^^^^
9 │ }
10 │

ℹ This object can be a new object on every render, even when none of its values have changed. Because of this, useCallback treats its dependencies as changed on every render.

ℹ Take the values that you need out of this object, and list those values as dependencies instead.

ℹ 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 { useEffect } from "react";
import { useQuery } from "@tanstack/react-query";
function useTodos() {
return useQuery({ queryKey: ["todos"], queryFn: fetchTodos });
}
function Component() {
const todos = useTodos();
useEffect(() => {
console.log(todos.data);
}, [todos]);
}
code-block.js:12:9 lint/nursery/noTanstackQueryUnstableDeps ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━

⚠ The object returned by useQuery is a dependency of useEffect.

10 │ useEffect(() => {
11 │ console.log(todos.data);
> 12 │ }, [todos]);
│ ^^^^^
13 │ }
14 │

ℹ This object can be a new object on every render, even when none of its values have changed. Because of this, useEffect treats its dependencies as changed on every render.

ℹ Take the values that you need out of this object, and list those values as dependencies instead.

ℹ 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 { useCallback, useEffect, useMemo } from "react";
import { useMutation, useQueries, useQuery } from "@tanstack/react-query";
function Component() {
const { mutate } = useMutation({ mutationFn: (value) => value });
const callback = useCallback(() => {
mutate("hello");
}, [mutate]);
const { data } = useQuery({ queryKey: ["todos"], queryFn: fetchTodos });
useEffect(() => {
console.log(data);
}, [data]);
const combined = useQueries({
queries: [{ queryKey: ["users"], queryFn: fetchUsers }],
combine: (results) => results.map((result) => result.data),
});
const count = useMemo(() => combined.length, [combined]);
}