Why does my optimistic update have a race condition even though I call cancelQueries? #7932
Replies: 1 comment 3 replies
-
The race condition probably comes from too many invalidations. I think this is what happens:
It's a rare case where the invalidation basically "overtake" the second mutation, and it will be consistent eventually, so I don't think it's a big deal. However, one good solution I'm teaching in my course is to not invalidate after every mutation, but only after the last one. You can tag them with a
Now this will only invalidate if there is only 1 mutation running (the single mutation itself) for that key, so in rapidly fired mutations, you'll only get one invalidate at the end, which is likely what you want anyways. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have implemented optimistic updates into my shopping cart. I noticed that when I modify different items in rapid succession, sometimes there is a glitch in the UI.
My optimistic mutation is structured as described in the documentation and I cancel the running query at the beginning of
onMutate
.Why do I still seem to receive outdated values?
If I comment out
invalidateQueries
, it seems to work. But then I don't get the latest cart data.Beta Was this translation helpful? Give feedback.
All reactions