What is mutationKey?
#6093
-
The queryKey shares the cache among shared queries. What role does mutationKey play? It would be nice if it were documented. |
Beta Was this translation helpful? Give feedback.
Answered by
Lisba
Oct 10, 2023
Replies: 1 comment 4 replies
-
Hi, @arcc1845! The For instance:
I hope this helps to clarify a little bit more the role of the mutation keys 😉. |
Beta Was this translation helpful? Give feedback.
4 replies
Answer selected by
arcc1845
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, @arcc1845! The
mutationKey
role is to link a mutation with a default option already configured or to identify a mutation to share its state like loading, error, or data across components.For instance:
mutationFn
for all the mutations with the key['addProduct']
and then you can just call theuseMutation
hook with the mutation key without needing nothing else to be specified, this way, you avoid writing a lot of code and reuses it instead:queryClient.setMutationDefaults(['addProduct'], { mutationFn: addProductFunction })
const mutation = useMutation({ mutationKey: ['addProduct'] })
.