Is there a way to keep reactive object up to date with the original one? #7811
-
Hi! Is there in Vue 3 a way to keep my reactive object up to date with the original one, like it worked in Vue 2? For instance:
I'm struggling with issue where I have an npm package with objects which are mutating themselves (for instance, However, Vue 2 works well with this case: reactive Is there a way to fix this issue? |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 9 replies
-
If you are a new project, it is recommended to use setup. demo |
Beta Was this translation helpful? Give feedback.
-
@chenxch That's an already existing relatively large project. I'm trying to move from Vue 2 to Vue 3 now. Actually, I have chats websocket and logic, provided by our npm lib. I can subscribe to these chats events like following:
At
Then, lib modifies this But in Vue 3 state
If I understand correctly, that's because reactive Proxy looses reference to original object (docs link). However may be there's some workarounds for this issue? |
Beta Was this translation helpful? Give feedback.
-
I hope there is should be an easy workaround solution, or I made some logical mistake, because, I suppose, this case is quite typical, and I don't know how to refactor it so it would work with new reactivity system which loses references to original objects :( Theoretically, I can move all lib logic to my side, but this isn't scaling at all so that I assume that as poor solution. But I cannot think up an another solution for this case :( |
Beta Was this translation helpful? Give feedback.
-
I think either your example is wrong, or you are mistunderstanding your problem. the code you shared will work insofar as the reactive The problem you might rather experience is that modifying the non-reactive And that's expected. Vue 3 Reactivity tracks changes through the reactive proxy. any changes done directly to the non-reactive original cannot be tracked. So it might appear to you as if the change was lost, simply because the UI could not react to the change. |
Beta Was this translation helpful? Give feedback.
-
So, I have an application written on Vue 2 where I work with chats. Chats are running through websocket and there is a layer between my application and this websocket: our client lib which wraps websocket handling and contains some logic shared between applications. Here, in my store, I subscribe to Client library chat events, like following:
I try to reference Client library as much as possible instead of writing my own logic in order to prevent logic duplication and to have only one source of truth. For instance, let's take messages. I do not push any messages to in my code because Client library does it for me in its
So I want to messages to be drawn automatically when they appear on external |
Beta Was this translation helpful? Give feedback.
So, I have an application written on Vue 2 where I work with chats. Chats are running through websocket and there is a layer between my application and this websocket: our client lib which wraps websocket handling and contains some logic shared between applications.
Here, in my store, I subscribe to Client library chat events, like following: