📖 Scenario: You are building a simple Vue app to manage a user's profile information. The profile has nested details like name and address. You want to understand how shallow ref and shallow reactive work to control reactivity on nested objects.
🎯 Goal: Create a Vue component that uses shallowRef and shallowReactive to hold a user profile object. Update nested properties and observe how reactivity behaves differently with shallow wrappers.
📋 What You'll Learn
Create a shallowRef holding a user profile object with nested properties
Create a shallowReactive holding a user profile object with nested properties
Update a nested property inside the shallowRef object
Update a nested property inside the shallowReactive object
Add template code to display the user name and address from both shallowRef and shallowReactive
💡 Why This Matters
🌍 Real World
Managing complex state objects in Vue apps where you want to optimize performance by limiting deep reactivity.
💼 Career
Understanding shallowRef and shallowReactive helps frontend developers write efficient Vue components and avoid unnecessary re-renders.
Progress0 / 4 steps
1
Setup user profile data with shallowRef
In the <script setup> block, import shallowRef from 'vue'. Create a variable called userShallowRef using shallowRef with this exact object: { name: 'Alice', address: { city: 'Wonderland', zip: '12345' } }.
Vue
Hint
Use import { shallowRef } from 'vue' and then const userShallowRef = shallowRef(...) with the exact object.
2
Add shallowReactive user profile data
Import shallowReactive from 'vue'. Create a variable called userShallowReactive using shallowReactive with this exact object: { name: 'Bob', address: { city: 'Builderland', zip: '54321' } }.
Vue
Hint
Import shallowReactive and create userShallowReactive with the exact object.
3
Update nested properties in shallowRef and shallowReactive
Update the city property inside the address of userShallowRef.value to 'New Wonderland'. Also update the city property inside the address of userShallowReactive to 'New Builderland'.
Vue
Hint
Remember to update nested properties inside userShallowRef.value and userShallowReactive.
4
Add template to display user names and cities
Add a <template> block that displays the name and address.city for both userShallowRef.value and userShallowReactive. Use {{ userShallowRef.value.name }} and {{ userShallowRef.value.address.city }} for shallowRef, and {{ userShallowReactive.name }} and {{ userShallowReactive.address.city }} for shallowReactive.
Vue
Hint
Use mustache syntax inside the template to show the names and cities from both objects.
Practice
(1/5)
1. What does shallowRef in Vue track reactively?
easy
A. All nested properties deeply inside the object
B. Only primitive values inside the object
C. Only changes to the reference itself, not nested properties
D. Changes only to arrays, not objects
Solution
Step 1: Understand shallowRef behavior
shallowRef tracks reactivity only when the reference changes, not inside nested objects.
Step 2: Compare with deep reactivity
Unlike ref or reactive, it does not track nested property changes.
Final Answer:
Only changes to the reference itself, not nested properties -> Option C
Quick Check:
shallowRef tracks reference, not deep changes [OK]
Hint: shallowRef tracks only the outer reference change [OK]
A. Nested property change won't trigger reactivity
B. Syntax error due to incorrect property access
C. state.value is undefined
D. Cannot assign to nested property inside shallowRef
Solution
Step 1: Analyze shallowRef behavior
shallowRef tracks only changes to the reference itself, not nested properties inside the object.
Step 2: Understand nested mutation effect
Changing state.value.nested.value updates the nested property but does not trigger Vue reactivity updates.
Final Answer:
Nested property change won't trigger reactivity -> Option A
Quick Check:
shallowRef ignores nested property changes [OK]
Hint: shallowRef ignores nested changes, only tracks reference [OK]
Common Mistakes:
Expecting nested changes to trigger updates
Confusing shallowRef with reactive
Thinking this causes syntax or runtime errors
5. You want to optimize performance by tracking only top-level changes in a complex object with nested data. Which approach correctly combines shallow reactivity with a watch to detect nested changes manually?