📖 Scenario: You are building a simple Vue 3 app to track a user's profile information. You want to see how Vue's reactivity system updates the UI when nested data changes.
🎯 Goal: Build a Vue 3 component that uses a reactive object with nested properties. Update nested properties and see the UI update automatically. This will help you understand why deep reactivity matters in Vue.
📋 What You'll Learn
Use Vue 3 Composition API with <script setup>
Create a reactive object with nested properties
Add a button to update a nested property
Display nested property values in the template
Observe UI updates when nested data changes
💡 Why This Matters
🌍 Real World
Understanding deep reactivity helps you build dynamic Vue apps where nested data changes reflect instantly in the UI, such as user profiles, settings, or complex forms.
💼 Career
Vue developers must grasp deep reactivity to debug UI update issues and write efficient, maintainable reactive code in real projects.
Progress0 / 4 steps
1
Create a reactive user profile object
Inside <script setup>, create a reactive object called userProfile with these exact nested properties: name set to "Alice", and address which itself is an object with city set to "Wonderland".
Vue
Hint
Use Vue's reactive function to create a reactive object with nested properties.
2
Add a button to update the nested city property
Add a function called changeCity inside <script setup> that changes userProfile.address.city to "Dreamland". Also add a button in the template with @click="changeCity" and label it Change City.
Vue
Hint
Create a function to update the nested city property and a button to trigger it.
3
Display the nested city property in the template
Inside the template, add a paragraph that shows the text Current city: followed by the value of userProfile.address.city using Vue's interpolation syntax.
Vue
Hint
Use double curly braces {{ }} to show the nested city property in the template.
4
Add the user name display and wrap content in a semantic section
In the template, add a heading <h2> that displays User: followed by userProfile.name. Wrap the button and paragraph inside a <section> element with aria-label="User Profile Section" for accessibility.
Vue
Hint
Use semantic HTML and add an aria-label for accessibility. Display the user name in an <h2> heading.
Practice
(1/5)
1. Why is deep reactivity important in Vue when working with nested objects?
easy
A. Because deep reactivity slows down the app and should be avoided.
B. Because Vue can track changes inside nested objects and update the UI accordingly.
C. Because Vue ignores nested objects and only tracks top-level properties.
D. Because Vue requires manual DOM updates for nested object changes.
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 B
Quick Check:
Deep reactivity = UI updates nested changes [OK]
Hint: Deep reactivity means nested data changes update UI automatically [OK]
Common Mistakes:
Thinking Vue only tracks top-level properties
Believing manual DOM updates are needed for nested changes
Assuming deep reactivity hurts performance always
2. Which of the following is the correct way to make a nested object reactive in Vue 3?
easy
A. const state = reactive({ user: { name: 'Alice' } })
B. const state = shallowRef({ user: { name: 'Alice' } })
C. const state = reactive({ user: ref({ name: 'Alice' }) })
D. const state = ref('user', { name: 'Alice' })
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 A
Quick Check:
Use reactive() for nested objects [OK]
Hint: Use reactive() for nested objects, ref() for primitives [OK]
Common Mistakes:
Using ref() for nested objects expecting deep reactivity
Wrapping nested objects again with reactive() unnecessarily
Incorrect ref() syntax with multiple arguments
3. Given the code:
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?
medium
A. Error: Cannot assign to read only property
B. 30
C. undefined
D. 31
Solution
Step 1: Understand reactive object behavior
The state object is reactive, so nested properties like profile.age are tracked and can be updated.
Step 2: Trace the updateAge function
The function sets state.profile.age to 31, changing the reactive property.