Reactivity helps Vue update the page when data changes. But some things don't work automatically, so you need to know the limits.
Reactivity caveats and limitations in Vue
const obj = reactive({ a: 1 }) delete obj.a // This won't be reactive // Correct way to delete reactive property Reflect.deleteProperty(obj, 'a') // triggers reactivity // For arrays const arr = reactive([1, 2, 3]) arr.length = 1 // direct length shortening is not reliably reactive arr.splice(1) // reactive way
Vue 3's reactive can track most changes but property deletion with delete and direct array length shortening may not trigger updates reliably.
Use Reflect.deleteProperty() for deletions and array methods like splice to keep changes reactive.
const state = reactive({ count: 0 }) state.count = 1 // reactive update
delete does not trigger reactivity.const state = reactive({ prop: 'hello' }) delete state.prop // not reactive
length assignment is not reliably reactive; use splice instead.const arr = reactive([1, 2, 3]) arr.length = 1 // not reliably reactive arr.splice(1) // reactive update
This Vue component shows reactivity caveats with deleting a property using delete and shortening an array by setting length directly vs. using splice. Direct methods do not update the UI.
<template>
<div>
<p>Count: {{ state.count }}</p>
<p>Prop: {{ state.prop }}</p>
<button @click="deleteProp">Delete Prop</button>
<p>Array: {{ arr }}</p>
<button @click="shortenArrayDirect">Shorten Array Directly</button>
<button @click="shortenArraySplice">Shorten Array with Splice</button>
</div>
</template>
<script setup>
import { reactive } from 'vue'
const state = reactive({ count: 0, prop: 'hello' })
const arr = reactive([1, 2, 3])
function deleteProp() {
delete state.prop
}
function shortenArrayDirect() {
arr.length = 1
}
function shortenArraySplice() {
arr.splice(1)
}
</script>Vue 3 improved reactivity but delete on properties and direct array length shortening do not reliably trigger updates.
Use Reflect.deleteProperty() for deletions and array methods like splice to ensure updates.
For deeply nested objects, consider using watch with deep option or toRefs for better tracking.
Vue reactivity tracks most changes but has limits with delete operator on properties and direct array length shortening.
Use Reflect.deleteProperty() and array methods like splice to keep UI updated.
Understanding these limits helps avoid bugs and keeps your app reactive.
Practice
Solution
Step 1: Understand Vue's reactivity on object properties
Vue cannot detect new properties added directly to an existing reactive object after its creation.Step 2: Compare options with this limitation
Adding a new property directly to an existing reactive object won't trigger updates correctly states this limitation. The other options are incorrect because reactive() supports nested objects, property changes do trigger updates (though batched asynchronously), and Vue has exceptions for some array changes like setting length.Final Answer:
Adding a new property directly to an existing reactive object won't trigger updates. -> Option CQuick Check:
New object properties not reactive = C [OK]
- Assuming all object property additions are reactive
- Thinking array index changes always trigger updates
- Believing reactive() disables nested reactivity
Solution
Step 1: Identify reactive array update methods
Directly assigning to an array index (like myArray[2] = ...) is reactive in Vue 3 for reactive arrays.Step 2: Evaluate each option
myArray[2] = 'new value' uses direct assignment which triggers reactivity in Vue 3. myArray = ['new value'] replaces the whole array. myArray.push('new value') adds an element at the end. myArray.$set(2, 'new value') uses Vue 2 syntax unavailable in Vue 3.Final Answer:
myArray[2] = 'new value' -> Option AQuick Check:
Direct array index updates reactive in Vue 3 = A [OK]
- Replacing the entire array instead of updating an element
- Using push() to update a specific index
- Using Vue 2 methods like $set in Vue 3
const state = reactive({ items: ['a', 'b', 'c'] });
state.items[1] = 'x';
console.log(state.items);What will be logged?
Solution
Step 1: Understand array index reactivity in Vue 3
In Vue 3, reactive arrays track direct index assignments reactively, so state.items[1] = 'x' updates the array.Step 2: Predict the console output
The array changes from ['a', 'b', 'c'] to ['a', 'x', 'c'], so logging state.items shows the updated array.Final Answer:
['a', 'x', 'c'] -> Option BQuick Check:
Vue 3 reactive arrays track index changes = A [OK]
- Thinking direct index assignment is not reactive in Vue 3
- Expecting an error on index assignment
- Confusing array push with index update
const obj = reactive({});
obj.newProp = 123;
console.log(obj.newProp);Solution
Step 1: Check adding new properties to reactive objects
Vue cannot detect new properties added directly to reactive objects after creation, so newProp is added but not reactive.Step 2: Understand console output and reactivity
newProp logs 123 correctly, but changes to it won't trigger UI updates because Vue didn't track it reactively.Final Answer:
newProp is added but changes won't trigger updates -> Option AQuick Check:
New object properties added later not tracked = B [OK]
- Assuming newProp is reactive immediately
- Expecting undefined instead of 123
- Thinking code throws an error
age to a reactive object user and ensure Vue tracks it reactively. Which approach is correct?Solution
Step 1: Understand Vue 3 reactivity and adding new properties
Vue 3 does not track new properties added directly to reactive objects. Using object spread creates a new reactive object with the new property.Step 2: Evaluate each option for reactivity
Direct property assignments do not make properties reactive. Vue.set(user, 'age', 30) is Vue 2 syntax and not available in Vue 3. user = { ...user, age: 30 } creates a new reactive object with age included, ensuring reactivity.Final Answer:
user = { ...user, age: 30 } -> Option DQuick Check:
Use object spread to add reactive properties = A [OK]
- Using Vue.set in Vue 3 (deprecated)
- Adding properties directly expecting reactivity
- Confusing direct assignment with reactive update
Ready to prove it?
Pass the test and this pattern moves unknown
