I'm creating a form component, and it needs to keep track of if the form has changed (if it's different to it's original value or not) - and to do this I need to store the initial prop value to compare to. This will be a reusable form component, so the value of the prop is unknown except for it being an object.
There has been a similar question for Vue 2 in the past, however the first method comes with a warning and doesn't seem to work anymore, the second is not applicable in Vue 3 and the third one is kind of what i'm doing now, however..
I'm deep cloning the prop, but to do so - I need to get rid of the proxy that Vue adds to the props, which is what i'm using `toRaw` for, however in the documentation for `toRaw()` it specifically says "It is not recommended to hold a persistent reference to the original object. Use with caution.", which is exactly what I want to do - keep a persistent reference to the original object.
const originalValue = structuredClone(toRaw(props.modelValue));
So my question is - what is the recommended way to go about storing initial values from props in cases like these? Is toRaw()
the right pick in this case?
Edit:
Another method that works is using JSON, but this could be unreliable for a flexible form if there are any circular dependencies in props.modelValue
.
const originalValue = JSON.parse(JSON.stringify(props.modelValue));