Reactivity Caveats
Explore the differences in reactivity systems between Vue 2 and Vue 3. Understand Vue 2's reactivity limitations such as property addition and array change detection, and discover how Vue 3's Proxy implementation resolves these issues. This lesson helps you grasp key migration challenges and enhancements in Vue 3's reactive data handling for large-scale applications.
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 ...