查詢函式必須回傳一個會被 TanStack Query 快取的值。沒有回傳值的函式(void 函式)可能導致非預期的行為,並可能表示實作中存在錯誤。
此規則的錯誤程式碼範例:
/* 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() // 函式未回傳取得的資料
},
})
此規則的正確程式碼範例:
/* 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
},
})