Framework
Version

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
  },
})

屬性

  • ✅ 推薦
  • 🔧 可自動修復