Reactivity Caveats
Learn how the reactivity system has changed in Vue 3.
We'll cover the following...
We'll cover the following...
One significant change coming with Vue 3 is the reactivity system implementation.
Vue 2
In Vue 2, to make data reactive, getters and setters of Object.defineProperty were used. However, this came with a few reactivity caveats.
For instance, to ensure the reactivity of a property on a data object, it must be added to the state during initialization. Vue 2 can’t detect property addition or deletion, as shown in the example below:
However, new properties can be added using the Vue.set method or the this.$set method available on Vue’s component instance.
Moreover, Vue 2 is unable to detect the following array changes:
- When setting an item using an index, like,
vm.items[itemIndex] = newValue - When modifying array length, like
vm.items.length = newLength
The first can be addressed by using the Vue.set or splice method ...