I'm using Vue3 where a lot of the objects are Proxy objects for reactivity. I want to create a deep copy of one of the proxy objects and recently discovered structuredClone.
https://developer.mozilla.org/en-US/docs/Web/API/structuredClone
When I run the following code, I get an error performing a structuredClone on proxyObj:
const obj = {
name: "Steve",
age: 50
}
const handler = {}
const proxyObj = new Proxy(obj, {})
console.log(proxyObj)
const objCopy = structuredClone(obj)
console.log(objCopy)
const proxyObjCopy = structuredClone(proxyObj)
console.log(objCopy)
Uncaught DOMException: Failed to execute 'structuredClone' on 'Window': # could not be cloned.
Is there a way I can clone the proxy object? Is there a way I can dereference it first, copy it, and not lose the reactivity? Any help is appreciated!
structuredClone({...proxyObj})
get you all the expected key-value pairs or is your the object more complex than the example you gave? – FootmanUncaught TypeError: Spread syntax requires ...iterable[Symbol.iterator] to be a function
– FletastructuredClone
returns a clone of the structure. It does loose reactivity by definition. – Advisable