optimistic update not updating dom
Im trying to optimistically update an item in an array but it doesnt trigger an update in the dom.
My response of the query is something like
and is fetched via the api, lets simply for now
now i have an update function where i want to update that user optimistically
Doing this will update the data that i see in the devtools, but wont trigger an update in the dom,
Updating a value in the root object will trigger an update
for example works
5 Replies
adverse-sapphireOP•15mo ago
it seems like the values arnt tracked deeply
resetting the value first and than set the expected value works, but this should not be the way right?
conscious-sapphire•15mo ago
You're correct:
data
in query isn't deeply tracked. You need to replace the whole object in order for Vue to detect this change and re-render. I believe data
is a shallowRef
under the hood.conscious-sapphire•15mo ago
I personally use
immer
library to simplify that. Basically you treat your data as immutable and use immer
to produce next immutable state. When optimistic update performance isn't a concern it looks clean. https://github.com/immerjs/immerGitHub
GitHub - immerjs/immer: Create the next immutable state by mutating...
Create the next immutable state by mutating the current one - immerjs/immer
conscious-sapphire•15mo ago
With
immer
your update can look like this:
One gotcha when using immer in Vue: need to disable object auto freeze. Otherwise Vue will produce warnings telling it's unable to setup its own proxies.
adverse-sapphireOP•15mo ago
Thanks! got it working by doing this instead