onSuccess and onError on useQuery do nothing after upgrading TanStack Query
onSuccess is not called in useQueryShort answer
TanStack Query v5 removed onSuccess, onError and onSettled from queries, so options passed to useQuery are ignored. Mutations still have them. React to query results with the returned data, error and status values, or an effect, and keep side effects on useMutation.
Fix it fast
1Find out which cause you have
Check the installed major version. Version 5 or higher explains the missing callbacks.
npm ls @tanstack/react-query2Match what you see, then jump to its fix
What does the error look like?
onSuccess is not called in useQueryonError callback not working after upgrading to React Query v5
Cause 1 of 2
The callbacks were removed from queries in v5
1Check
The migration guide states that onSuccess, onError and onSettled have been removed from queries.
grep -rn "onSuccess\|onError\|onSettled" --include="*.ts" --include="*.tsx" src2Fix
Read the result values instead. Run side effects in an effect that depends on data, or on error, and render error states from isError.
const { data, error, isError } = useQuery({
queryKey: ["projects"],
queryFn: fetchProjects,
});
useEffect(() => {
if (data) console.log("loaded", data.length);
}, [data]);3Verify
Open the page and watch the effect run when data arrives.
npm run devConfirmed by tanstack.com
Cause 2 of 2
The callback belongs on a mutation, not a query
1Check
Mutations were not changed in v5. onSuccess, onError and onSettled work on useMutation, and can also be passed per call to mutate().
grep -rn "useMutation" --include="*.ts" --include="*.tsx" src2Fix
Move the callback to useMutation. Call-level callbacks run after the hook-level ones, and do not run if the component unmounts first.
const queryClient = useQueryClient();
const mutation = useMutation({
mutationFn: addTodo,
onSuccess: () => queryClient.invalidateQueries({ queryKey: ["todos"] }),
});3Verify
Trigger the mutation and confirm the list refetches.
mutation.mutate(newTodo);Confirmed by tanstack.com
How do I stop it coming back?
- Read the migration guide's list of removed options before a major upgrade.
- Keep write side effects on mutations, and derive UI from query state.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- TaskvareA task and project-management suite: React on the web, Flutter on mobile, TypeScript underneath.
Related fixes
Web apps
TanStack Query says the query function returned undefined
Query data cannot be undefined. Please make sure to return a value other than undefined from your query function
TanStack Query treats a query that resolves to undefined as failed.
2 causes, each with a fixWeb apps
ESLint says an element in a list is missing its key prop
react/jsx-key
react/jsx-key fires when an element returned from map, Array.from or an array literal has no key.
3 causes, each with a fixWeb apps
There is no tailwind.config.js after installing Tailwind 4 and HeroUI in Next.js
HeroUI components render unstyled
Nothing is broken: Tailwind CSS 4 is configured in CSS and no longer creates tailwind.config.js.
3 causes, each with a fixWeb apps
react-i18next shows the wrong plural form for a count, or prints the key instead of the text
t('item') prints the key item instead of a translated sentence
i18next picks a plural from the count option and looks for keys ending in _one, _other and so on.
4 causes, each with a fixWeb apps
useSession throws because there is no SessionProvider above it
[next-auth]: `useSession` must be wrapped in a <SessionProvider />
useSession reads the session from React Context, and SessionProvider is what creates that context.
3 causes, each with a fixWeb apps
next/link throws React.Children.only on its children
Error: React.Children.only expected to receive a single React element child.
React.Children.only accepts exactly one React element and throws for anything else, including an array with one item.
3 causes, each with a fix