noTanstackQueryVoidQueryFn (JavaScript)
Summary
Section titled “Summary”- Rule available since:
v2.6.5 - Diagnostic Category:
lint/nursery/noTanstackQueryVoidQueryFn - This rule doesn’t have a fix.
- The default severity of this rule is error.
- This rule belongs to the following domains:
- Sources:
- Same as
@tanstack/query/no-void-query-fn
- Same as
How to configure
Section titled “How to configure”{ "linter": { "rules": { "nursery": { "noTanstackQueryVoidQueryFn": "error" } } }}Description
Section titled “Description”Disallow TanStack Query functions that don’t return any data.
A query function is the function that you give to TanStack Query as queryFn, for
example in the options of useQuery. TanStack Query saves the value that this function
returns as the data of the query. If the function returns a promise, TanStack Query waits
for the promise and saves its result instead.
TanStack Query doesn’t accept undefined as data. When a query function returns
undefined, the query fails with an error. This usually happens because a return
statement is missing, or because the function returns a value in some cases but not in
others.
This rule checks every object property named queryFn. It reports the property when the
function can return undefined, or a promise whose result is undefined.
When the function is written in the property itself, the rule checks every return
statement and whether the end of the function can be reached. When the property refers to
a function that is defined somewhere else, the rule only reports it if the function never
returns any data.
If a query has no data to return, return null instead.
Examples
Section titled “Examples”Invalid
Section titled “Invalid”import { useQuery } from "@tanstack/react-query";
useQuery({ queryKey: ["todos"], queryFn: async () => { await fetch("/api/todos"); },});/invalid-missing-return.js:5:5 lint/nursery/noTanstackQueryVoidQueryFn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This query function can return undefined.
3 │ useQuery({
4 │ queryKey: ["todos"],
> 5 │ queryFn: async () => {
│ ^^^^^^^
6 │ await fetch("/api/todos");
7 │ },
ℹ TanStack Query doesn't accept undefined as the data of a query. The query fails with an error instead of saving a result.
ℹ Return the data in every case. If there is no data to return, return null 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 { useQuery } from "@tanstack/react-query";
useQuery({ queryKey: ["todos"], queryFn: async () => { const response = await fetch("/api/todos"); if (response.ok) { return response.json(); } },});/invalid-missing-return-in-one-path.js:5:5 lint/nursery/noTanstackQueryVoidQueryFn ━━━━━━━━━━━━━━━━━
✖ This query function can return undefined.
3 │ useQuery({
4 │ queryKey: ["todos"],
> 5 │ queryFn: async () => {
│ ^^^^^^^
6 │ const response = await fetch("/api/todos");
7 │ if (response.ok) {
ℹ TanStack Query doesn't accept undefined as the data of a query. The query fails with an error instead of saving a result.
ℹ Return the data in every case. If there is no data to return, return null 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 { queryOptions } from "@tanstack/react-query";
async function refreshTodos(): Promise<void> { await fetch("/api/todos/refresh");}
const options = queryOptions({ queryKey: ["todos"], queryFn: refreshTodos,});/invalid-void-function.ts:9:5 lint/nursery/noTanstackQueryVoidQueryFn ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✖ This query function can return undefined.
7 │ const options = queryOptions({
8 │ queryKey: ["todos"],
> 9 │ queryFn: refreshTodos,
│ ^^^^^^^
10 │ });
11 │
ℹ TanStack Query doesn't accept undefined as the data of a query. The query fails with an error instead of saving a result.
ℹ Return the data in every case. If there is no data to return, return null 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 { queryOptions, useQuery } from "@tanstack/react-query";
useQuery({ queryKey: ["todos"], queryFn: async () => { const response = await fetch("/api/todos"); return response.json(); },});
useQuery({ queryKey: ["selected-todo"], queryFn: () => null,});
async function countTodos(): Promise<number> { return 3;}
const options = queryOptions({ queryKey: ["todo-count"], queryFn: countTodos,});Related links
Section titled “Related links”Copyright (c) 2023-present Biome Developers and Contributors.