Skip to content

isPending remains true if error is thrown in onError of mutate and a query is invalidated on settle #9664

@SimonSimCity

Description

@SimonSimCity

Describe the bug

The isPending state of a mutation does not reset to false but remains true if an error is thrown in the onError function of the mutate function, and the error did originate either within the mutationFn or in the onSuccess passed to the useMutate function.

Furthermore, the queryClient must be configured to invalidate all queries when a mutation settles, and a query must be present in the tree.

In this exact scenario, I could reproduce this bug. When deviating from this setting, the isPending state of the mutation did reset to false as one would expect.

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-query-bwruozre?file=src%2Findex.tsx&preset=node

Steps to reproduce

  1. Open the provided link (https://stackblitz.com/edit/tanstack-query-bwruozre?file=src%2Findex.tsx&preset=node)
  2. Click on the button "Action!"

Expected behavior

The value of isPending of my mutation should be false again - as it does if you remove line 69, which throws the error inside the onError function of the mutate call.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: macOS
  • Browser: Chrome v140.0.7339.133

Tanstack Query adapter

react-query

TanStack Query version

5.89.0

TypeScript version

5.9.2

Additional context

I found this error while trying to check what would happen if an onSuccess or onError callback would throw an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions