How to share useMutation() return value across components?
Hi guys, I'm just starting out with TSQ!
I have a component that's calling useMutation. However, I want to access its return value (specifically .isPending and .variables) in a faraway component. Is this achievable without prop-drilling or a custom context? Thanks 🙂
4 Replies
stormy-gold•3y ago
With
useMutationStatevicious-goldOP•3y ago
Hi there!
I looked into this!
I want to check if a specific mutation call is pending and get that specific calls variables.
However, since
useMutationState returns an array, I end up getting an array of each mutation.state for each of the calls.
This leaves me no way to identify the specific mutation.state that I want to read from. Any ideas?
I have thought that perhaps after each mutation call succeeds, I could clear that particular mutation cache, meaning that useMutationState() will theoretically always return an array of length 1.
Any help is appreciated! 🙂ambitious-aqua•3y ago
Give the mutation a
mutationKey and filter by that key when calling useMutationState.
See https://github.com/TanStack/query/discussions/6118#discussioncomment-7227206stormy-gold•3y ago
It will still be an array because you could have multiple mutations for the same key.