How do you handle concurrent optimistic updates without older responses overwriting newer data? #11408
Unanswered
kuldeeprajput-dev
asked this question in
Q&A
Replies: 2 comments
|
If the same item can be mutated more than once, give the mutation a shared Keep the normal optimistic-update pattern in await queryClient.cancelQueries({ queryKey: ['item', id] })
const previous = queryClient.getQueryData(['item', id])
queryClient.setQueryData(['item', id], optimistic)
|
0 replies
|
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
I have multiple mutations updating the same query cache.
For example, if a user quickly updates the same item twice:
I update the cache optimistically in onMutate and invalidate the query in onSettled.
The problem is that sometimes the first request finishes after the second one, and the older response/refetch can overwrite the latest state.
What is the recommended way to handle this in TanStack Query?
Should I cancel queries, track mutation versions, avoid invalidating after every mutation, or handle this some other way?
All reactions