react-doctor/query-no-usequery-for-mutation
Use `useMutation()` for POST/PUT/DELETE — it provides onSuccess/onError callbacks, doesn't auto-refetch, and correctly models write operations
- Category: TanStack Query
- Severity: warn
- Source:
oxlint-plugin-react-doctor - Framework: tanstack-query
- Enabled when: framework=tanstack-query and capabilities=tanstack-query
Validation prompt
Use this to decide whether a fired diagnostic is real or a false positive.
Fires on useQuery|useInfiniteQuery|useSuspenseQuery|useSuspenseInfiniteQuery({ queryFn }) when queryFn's body contains a fetch(url, { method: 'POST'|'PUT'|'DELETE'|'PATCH' }) call with a literal method string. Wrapper clients like axios.post(...) or ky.post(...) won't match — only the bare global fetch identifier paired with a literal method property.
Fix prompt
Use this once validation confirms the diagnostic is real.
Swap useQuery for useMutation({ mutationFn }) and trigger it from an event handler via mutation.mutate(input). useMutation exposes onSuccess/onError/onSettled, doesn't auto-refetch on focus/mount/reconnect, and won't fire on render — exactly the semantics writes need. https://tanstack.com/query/latest/docs/framework/react/guides/mutations
Related rules
More TanStack Query rules from the rules reference:
react-doctor/query-no-void-query-fn: queryFn must return a value for the cache. Use the `enabled` option to conditionally disable the query instead of returning undefinedreact-doctor/query-stable-query-client: Move `new QueryClient()` to module scope or wrap in `useState(() => new QueryClient())` — recreating it on every render resets the entire cachereact-doctor/query-destructure-result: Destructure the query result instead of binding the whole object: const { data, isLoading } = useQuery(...)react-doctor/query-mutation-missing-invalidation: Add `onSuccess: () => queryClient.invalidateQueries({ queryKey: ['...'] })` so cached data stays in sync after the mutationreact-doctor/query-no-query-in-effect: React Query manages refetching automatically via queryKey dependencies and the `enabled` option — manual refetch() in useEffect is usually unnecessary