Replies: 1 comment 2 replies
-
(1) I think the idiomatic way to have an instance that you can reactively swap out for another is to put it in a ref. It should also be more performant, by far: const formData = ref({ firstFormData: 'firstFormData' })
formData.value = { secondFormData: 'secondFormData' } (2) It is a bad practice to define new properties on objects you don't control. (3) The implementation has edge cases that won't work. This is ok in a project codebase but not in a general lib like Vue. For example, if I have added a property to my object as non-configurable, the To me, it feels like this belongs to a |
Beta Was this translation helpful? Give feedback.
-
Summary
The request is to add new non-enumerable method
.replace()
on reactive to update object via native api.Motivation
It's well known and not documented that in case we want to fully update (replace) object in reactive, we must use:
We could avoid "Object.assign" if manually wrapping our data object in some kind of
{ value: { ...data } }
.Thus, reactiveObject.value can be safely replaced.
Or as suggested in article below we can use deepAssign recursive function.
Related information
Also, VueUse provides utilities such as useReactive (a shorthand for creating reactive objects) and reactivePick or reactiveOmit for deriving new reactive objects based on existing ones. However, these utilities don’t directly replace the entire reactive object - they create or return new reactive instances.
Basic example
Detailed design
Treat this as merely implementation idea/example:
Drawbacks
Method attachment can lead to unexpected bugs.
We should assure that the reactive object will still behave as expected, and "replace" won’t interfere with Vue’s reactivity tracking.
Beta Was this translation helpful? Give feedback.
All reactions