Describe the bug#
When using mutation filters in react-query v3, mutation variables were accessible through mutation.options.variables. Upon migrating to v4, it seems that variables should be accessed through mutation.state since mutation.options.variables can be undefined.
Looking at the source code, it seems that both mutation.options and mutation.state have variables as an optional property. The docs seem to only provide an example of filtering mutations via variables using options, and no mention of state.
What is the distinction between options and state in regards to mutation filters? Prior to v4, mutation.options.variables always existed, but can now be undefined in v4.
Your minimal, reproducible example#
https://codesandbox.io/s/peaceful-euclid-5876nl?file=/src/App.js
Steps to reproduce#
- Set up
useMutation and useIsMutating using the same mutation key
- Trigger a mutation and provide variables
- In
useIsMutating, add a predicate options and log mutation.options.variables and mutation.state.variables
- See that the former can be undefined, whereas the latter always contains the mutation variables
Expected behavior#
As a user, I expected to consistently see mutation variables in mutation.options.variables but I am only consistently seeing the variables in mutation.options.state.
How often does this bug happen?#
Every time
Screenshots or Videos#
No response
Platform#
macOS - Chrome - v121.0.6167.160
Tanstack Query adapter#
react-query
TanStack Query version#
v4.36.1
TypeScript version#
v5.3.3
Additional context#
No response
I think we have removed
mutation.option.variablesin v5. The variables aren't defined when the mutation is defined, which is why they can't exist on options. So I wouldn't use them - just the ones onstate.On the
stateof the mutation, it can potentiallyundefinedbecause you could theoretically check theMutationafter it has been created, but before it has been invoked. In react, the adapter makes sure to create-and-execute a mutation, so technically, it shouldn't happen that it's undefined, but on type level, it can be.So please use
mutation.state.variablesand make sure to check if it's defined.