Understanding deep reactivity helps you make your Vue app update correctly when data changes, even inside nested objects or arrays.
Why deep reactivity understanding matters in Vue
import { reactive } from 'vue'; const state = reactive({ user: { name: 'Alice', age: 30 } }); // Changing nested property state.user.age = 31;
reactive() makes an object deeply reactive by default.
Changes inside nested objects or arrays are tracked automatically.
const state = reactive({ count: 0 }); state.count = 1;
const state = reactive({ user: { name: 'Bob' } }); state.user.name = 'Robert';
const state = reactive({ items: [1, 2, 3] }); state.items.push(4);
This Vue component shows a user's age and a button to increase it. Because state is deeply reactive, changing state.user.age updates the displayed age automatically.
<template>
<div>
<p>User age: {{ state.user.age }}</p>
<button @click="increaseAge">Increase Age</button>
</div>
</template>
<script setup>
import { reactive } from 'vue';
const state = reactive({
user: {
age: 25
}
});
function increaseAge() {
state.user.age++;
}
</script>Vue's reactive() tracks nested changes automatically using Proxies. ref() provides the same deep reactivity when the ref holds an object.
Direct index or length mutations on arrays may not trigger updates; prefer using array mutation methods like push(), splice(), etc.
Use Vue DevTools to inspect reactivity and see what changes trigger updates.
Deep reactivity means Vue tracks changes inside nested objects and arrays.
This helps your UI update correctly when nested data changes.
Understanding this avoids bugs and improves app reliability.
Practice
Solution
Step 1: Understand Vue's reactivity system
Vue tracks changes in data to update the UI automatically. Deep reactivity means it watches nested objects, not just top-level properties.Step 2: Recognize the benefit of deep reactivity
This allows the UI to reflect changes inside nested objects without extra code, preventing bugs and keeping the app reliable.Final Answer:
Because Vue can track changes inside nested objects and update the UI accordingly. -> Option BQuick Check:
Deep reactivity = UI updates nested changes [OK]
- Thinking Vue only tracks top-level properties
- Believing manual DOM updates are needed for nested changes
- Assuming deep reactivity hurts performance always
Solution
Step 1: Recall Vue 3 reactivity APIs
Vue 3 uses reactive() for deep reactive objects and ref() for primitive or shallow reactive references.Step 2: Identify correct nested reactive syntax
Using reactive() on an object with nested objects makes all nested properties reactive automatically.Final Answer:
const state = reactive({ user: { name: 'Alice' } }) -> Option AQuick Check:
Use reactive() for nested objects [OK]
- Using ref() for nested objects expecting deep reactivity
- Wrapping nested objects again with reactive() unnecessarily
- Incorrect ref() syntax with multiple arguments
const state = reactive({ profile: { age: 30 } })
function updateAge() {
state.profile.age = 31
}
updateAge()
console.log(state.profile.age)What will be logged to the console?
Solution
Step 1: Understand reactive object behavior
Thestateobject is reactive, so nested properties likeprofile.ageare tracked and can be updated.Step 2: Trace the updateAge function
The function setsstate.profile.ageto 31, changing the reactive property.Final Answer:
31 -> Option DQuick Check:
Reactive nested property updates reflect immediately [OK]
- Expecting old value because nested reactivity is misunderstood
- Thinking assignment causes error on reactive objects
- Confusing reactive with readonly objects
const state = reactive({ items: [{ id: 1, name: 'A' }] })
function addItem() {
state.items.push({ id: 2, name: 'B' })
}
addItem()
console.log(state.items.length)What is the likely issue if the UI does not update after calling
addItem()?Solution
Step 1: Check how reactivity applies to arrays
Vue's reactive() tracks arrays deeply, but if the array was not reactive, push won't trigger updates.Step 2: Identify common mistake
Ifitemswas not wrapped in reactive(), Vue won't detect push changes, so UI won't update.Final Answer:
The array was not made reactive properly, so Vue can't track push changes. -> Option AQuick Check:
Arrays must be reactive to track push/pop [OK]
- Assuming push is non-reactive in Vue
- Replacing whole array unnecessarily
- Believing Vue only tracks primitives
const state = reactive({
user: {
profile: {
settings: { theme: 'light' }
}
}
})
// Later in code
state.user.profile.settings.theme = 'dark'Why might Vue fail to update the UI if
settings was added later like this?const settings = { theme: 'light' }
state.user.profile.settings = settingsSolution
Step 1: Understand reactivity on replaced nested objects
Replacing a nested object directly breaks reactivity unless the new object is made reactive.Step 2: Recognize Vue 3 solution
In Vue 3, you must assign a reactive object or use reactive() when replacing nested objects to keep reactivity.Final Answer:
Because replacing a nested object after initialization breaks reactivity unless done with reactive(). -> Option CQuick Check:
Replace nested objects with reactive() to keep reactivity [OK]
- Assuming Vue tracks replaced nested objects automatically
- Thinking Vue only tracks top-level changes
- Believing primitives are not reactive
Ready to prove it?
Pass the test and this pattern moves unknown
