Skip to content

Why useMutation is designed as not able to share status and data between multiple component? #6118

Answered by TkDodo
Brandon-Ln asked this question in Q&A
Discussion options

You must be logged in to vote

Why useMutation is designed not like a global store via mutation key?

Because every time you invoke a mutation by calling mutate or mutateAsync, you create a new mutation. So if you mutate twice, there will be two mutations for the same key. That's a fundamental difference to queries, where there can only ever be one entry for a key, and multiple calls to the queryFn will be deduplicated. There is no deduplication for mutations. If you do mutate('foo') and mutate('bar'), it will be two requests, and two mutations in the MutationCache.

What is the recommended practice in this common situation?

Usually, you use a mutation to make a change on the server, then you have a query to request …

Replies: 1 comment 2 replies

Comment options

You must be logged in to vote
2 replies
@Brandon-Ln
Comment options

@TkDodo
Comment options

Answer selected by Brandon-Ln
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants