Skip to content
Source
Sign inSign up
TanStack/query

Mutation cache, old mutations gets updated key and meta on state updates #6839

Closedgautemo(GitHub profile, opens in a new tab) opened this issue 2 comments

Describe the bug

The meta and mutationKey on previous mutations get updated when state in a component is updated. This only happens the first time the state updates and only on the last mutation in the cache.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-pchfwb?file=src%2FApp.tsx

Steps to reproduce

  1. Click the button "Run mutation"
  2. See the website list the mutation: "0 = 0 = 0 is true" ([value] = [key] = [meta] is [equal])
  3. Click the button "Bump state"
  4. See the website list the mutation: "0 = 1 = 1 is false"

Expected behavior

I expect that all existing mutations should not be altered when some state updates. Meaning step 4. should stay "0 = 0 = 0 is true". Meaning that the mutation key and meta object should still hold the value 0 that was the value when the mutation happened.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: Windows
  • Browser: Edge
  • Version: 119.0.2151.58

Tanstack Query adapter

react-query

TanStack Query version

5.18.1

TypeScript version

No response

Additional context

You can also see the values being updated in the ReactQueryDevtools.

  1. TkDodo added labelsbug

  2. TkDodo added labelspackage: query-core

  3. TkDodo closed this

  4. gautemo(GitHub profile, opens in a new tab)commented

    I see it is fixed when the key is updated. But I would also assume meta should not be updated on old mutations. This is actually happening when a key is missing or the same.

    Your minimal, reproducible example

    https://stackblitz.com/edit/vitejs-vite-jpzeoh?file=src%2FApp.tsx

    Steps to reproduce

    1. Click the button "Run mutation"
    2. See the website list the mutation: "0 = 0 is true" ([value] = [meta] is [equal])
    3. Click the button "Bump state"
    4. Click the button "Bump state"
    5. See the website list the mutation: "0 = 2 is false"

    Expected behavior

    I expect that all existing mutations should not be altered when some state updates. Meaning step 5. should stay "0 = 0 is true". Meaning that the meta object should still hold the value 0 that was the value when the mutation happened.

    Does my reasoning and what I expect sound like what should happen, @TkDodo? Also not that meta is only updated every second time, bug is not triggered on odd number of "Bump state" clicks.

  5. the currently observed mutation will get updates, I think that's expected. Otherwise, we would run into stale closure issues, like the one we fixed here:

    we could maybe not update the mutation if it is already completed ? If you want to contribute this, I think another check here would be needed:

    https://github.com/TanStack/query/blob/61f4d747883ce67cf4edcc65f9a7693fa2bc7b6a/packages/query-core/src/mutationObserver.ts#L77

Sign in to join this conversation.