框架
版本

Disallow returning void from query functions

查询函数必须返回一个将被 TanStack Query 缓存的值。不返回值的函数(void 函数)可能导致意外行为,并可能表明实现中存在错误。

规则详情

该规则的错误代码示例:

tsx
/* eslint "@tanstack/query/no-void-query-fn": "error" */

useQuery({
  queryKey: ['todos'],
  queryFn: async () => {
    await api.todos.fetch() // 函数未返回获取的数据
  },
})
/* eslint "@tanstack/query/no-void-query-fn": "error" */

useQuery({
  queryKey: ['todos'],
  queryFn: async () => {
    await api.todos.fetch() // 函数未返回获取的数据
  },
})

该规则的正确代码示例:

tsx
/* eslint "@tanstack/query/no-void-query-fn": "error" */
useQuery({
  queryKey: ['todos'],
  queryFn: async () => {
    const todos = await api.todos.fetch()
    return todos
  },
})
/* eslint "@tanstack/query/no-void-query-fn": "error" */
useQuery({
  queryKey: ['todos'],
  queryFn: async () => {
    const todos = await api.todos.fetch()
    return todos
  },
})

属性

  • ✅ 推荐
  • 🔧 可修复