T
TanStack3y ago
eager-peach

Pass information from onMutate to mutationFn

Is it possible to access the data returned by the onMutate function inside of the mutationFn function? Context: I am using the onMutate function to do optimistic updates. While doing so, I append a little information to my object, which I would like to use when syncing with the server inside of the mutationFn function.
2 Replies
xenophobic-harlequin
xenophobic-harlequin3y ago
No i don't think that's currently possible
eager-peach
eager-peachOP3y ago
I found a workaround but it's not very pretty: 1. In the onMutate method I change the latest mutation and append this information:
const mutation = queryClient.getMutationCache().getAll().at(-1);
mutation?.setState({ ...mutation.state, variables: { ...mutation.state.variables, extraInformation } });
const mutation = queryClient.getMutationCache().getAll().at(-1);
mutation?.setState({ ...mutation.state, variables: { ...mutation.state.variables, extraInformation } });
2. Now the mutationFn has access to this appended state (it’s given this information as its argument)

Did you find this page helpful?