Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
gautemo opened this issue Feb 5, 2024 · 2 comments · Fixed by #6889 or #7104
Closed

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

gautemo opened this issue Feb 5, 2024 · 2 comments · Fixed by #6889 or #7104
Labels
bug Something isn't working package: query-core

Comments

@gautemo
Copy link
Contributor

gautemo commented Feb 5, 2024

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.

@gautemo
Copy link
Contributor Author

gautemo commented Mar 8, 2024

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.

@TkDodo
Copy link
Collaborator

TkDodo commented Mar 11, 2024

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:

this.#currentMutation?.setOptions(this.options)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working package: query-core
Projects
None yet
2 participants