Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

onSuccess and onError on useQuery do nothing after upgrading TanStack Query

ErroronSuccess is not called in useQuery

Short 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.

  • Applies to TanStack Query 5, React 19, Next.js 15
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check the installed major version. Version 5 or higher explains the missing callbacks.

    npm ls @tanstack/react-query
  2. 2Match what you see, then jump to its fix

    1. 1The code worked on v4 and silently does nothing since the upgrade; the callback is passed to useQuery.The callbacks were removed from queries in v5Fix
    2. 2The side effect is meant to run after a POST or other write, and it was placed on useQuery.The callback belongs on a mutation, not a queryFix

What does the error look like?

  • onSuccess is not called in useQuery
  • onError 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" src

2Fix

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 dev

Confirmed by tanstack.com

Did this fix it?

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" src

2Fix

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

Did this fix it?

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:

Related fixes

Web 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

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.