Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

davidifranco's avatar

Creating non reactive copies of vue data

I want to have the ability to allow users to cancel a form change and revert back to the original form data. Whenever i assign old Data to the existing data i.e.

this.oldForm = this.form

the oldForm stays reactive to changes that occur in this.form. So if the user changes this.form.... this.oldForm is also updated.

is there a way to copy the original form data so it wont be reactive?

0 likes
7 replies
sikhlana's avatar

You can try:

this.oldForm = JSON.parse(JSON.stringify(this.form));

This is what I am using for my applications to remove the "reactiveness".

6 likes
devonblzx's avatar

PHP and Javascript objects are both assigned by reference.

In PHP, to create a new object, you would clone it.

$this->oldForm = clone $this->form

In Javascript, you can use the Object.assign call.

this.oldForm = Object.assign({}, this.form);

This will take your newly created object ({}) and assign all the values from this.form to it.

Note: sikhlana's solution is a good workaround when you need to deep copy an object (an object of objects).

devk's avatar

Or just Object.assign(this.oldForm, this.form). Or this.oldForm = {...this.form}.

rawilk's avatar

I like to use Object.assign() as mentioned above, or if you use a library such as lodash, you can use _.clone(obj) or _.cloneDeep(obj).

3 likes

Please or to participate in this conversation.