[Vuejs]-How to stop reactivity of variable in vue 3?

1πŸ‘

βœ…

To achieve this, You can use the toRaw function to obtain the original non-reactive version of the formData object and then use the reactive function to create a reactive copy of the formData object and assign to submitData variable.

Demo :

import { reactive, toRaw } from 'vue';

const formData = {...}

const submitData = reactive({ ...toRaw(formData) });

delete submitData.key;
πŸ‘€Debug Diva

0πŸ‘

You can create a shallow copy of formData

const submitData = { ...formData };

Or deep copy using cloneDeep from lodash

const submitData = _.cloneDeep(formData);

Both will create a new object with the same properties and values as the original object. However, the new object is a separate entity in memory, distinct from the original object. Modifications made to the copy won’t affect the original formData object.

πŸ‘€Tony

Leave a comment