Skip to content

Event handlers have stale state #5056

Closed
@srph

Description

@srph

Describe the bug

When the onSuccess handler for useMutation fires, you will have stale values of the state used inside onSuccess.

My thinking is that useMutation probably uses the value of onSuccess when mutate() is ran.

Your minimal, reproducible example

https://codesandbox.io/s/focused-buck-fxbe50?file=/src/App.tsx:966-1008

Steps to reproduce

  • Open Devtools
  • Press "Mutate" button
  • While the mutation is on-going, press the "Close" button

Expected behavior

As a user, I expected isOpen to be false. Instead, I got true -- which was the value of isOpen when the mutation ran.

How often does this bug happen?

Every time

Screenshots or Videos

image

Platform

  • OS: macOS Monterey
  • Browser: 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36'

TanStack Query version

v3.39.3

TypeScript version

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions