Discover how to speed up your Vue app by watching only what really matters!
Why Shallow ref and shallow reactive in Vue? - Purpose & Use Cases
Imagine you have a big object with many nested properties, and you want to track changes only on the top level, not deep inside. Manually checking every nested property for changes is like trying to watch every leaf on a huge tree for movement.
Manually tracking deep changes is slow and complicated. It can cause your app to react too often, making it laggy. Also, it's easy to miss updates or cause bugs because you forgot to watch some nested parts.
Shallow ref and shallow reactive let you watch only the top layer of your data. This means Vue tracks changes on the main object but ignores deep nested parts, making your app faster and simpler to manage.
const data = reactive({ user: { name: 'Alice', age: 30 } }); // tracks all nested changesconst data = shallowReactive({ user: { name: 'Alice', age: 30 } }); // tracks only top-level changesThis lets you build faster, more efficient apps by controlling exactly what changes Vue should react to.
Think of a social media app where you only want to update the user profile header when the main user object changes, but not every tiny detail inside their settings.
Manual deep tracking is slow and error-prone.
Shallow ref/reactive track only top-level changes.
This improves app speed and simplifies state management.
Practice
shallowRef in Vue track reactively?Solution
Step 1: Understand shallowRef behavior
shallowReftracks reactivity only when the reference changes, not inside nested objects.Step 2: Compare with deep reactivity
Unlikereforreactive, it does not track nested property changes.Final Answer:
Only changes to the reference itself, not nested properties -> Option CQuick Check:
shallowRef tracks reference, not deep changes [OK]
- Thinking shallowRef tracks nested property changes
- Confusing shallowRef with reactive
- Assuming shallowRef tracks primitive values inside
Solution
Step 1: Identify shallow reactive syntax
shallowReactiveis used to create a shallow reactive object that tracks only top-level properties.Step 2: Differentiate from other APIs
reactivecreates deep reactivity,shallowRefandrefcreate refs, not shallow reactive objects.Final Answer:
const state = shallowReactive({ count: 0, nested: { value: 1 } }) -> Option BQuick Check:
shallowReactive creates shallow reactive objects [OK]
- Using reactive() instead of shallowReactive()
- Confusing shallowRef with shallowReactive
- Using ref() for reactive objects
const state = shallowReactive({ count: 0, nested: { value: 1 } });
state.nested.value = 2;
console.log(state.nested.value);What will be logged to the console?
Solution
Step 1: Understand shallowReactive behavior
shallowReactive tracks only top-level properties reactively, but nested objects still exist and can be mutated normally.Step 2: Check nested property assignment
Assigningstate.nested.value = 2changes the nested object property directly, so the value updates.Final Answer:
2 -> Option DQuick Check:
Nested objects update but are not reactive [OK]
- Assuming nested changes are not possible
- Expecting nested changes to trigger reactivity
- Thinking assignment causes error
const state = shallowRef({ count: 0, nested: { value: 1 } });
state.value.nested.value = 5;
console.log(state.value.nested.value);Solution
Step 1: Analyze shallowRef behavior
shallowReftracks only changes to the reference itself, not nested properties inside the object.Step 2: Understand nested mutation effect
Changingstate.value.nested.valueupdates the nested property but does not trigger Vue reactivity updates.Final Answer:
Nested property change won't trigger reactivity -> Option AQuick Check:
shallowRef ignores nested property changes [OK]
- Expecting nested changes to trigger updates
- Confusing shallowRef with reactive
- Thinking this causes syntax or runtime errors
const state = shallowReactive({ user: { name: 'Alice', age: 30 } });
watch(() => state.user, (newVal, oldVal) => {
console.log('User changed:', newVal);
});
state.user.name = 'Bob';What is the expected behavior?
Solution
Step 1: Understand shallowReactive and watch
shallowReactivetracks only top-level properties reactively. The watch onstate.usertriggers only if theuserobject reference changes.Step 2: Analyze nested property mutation
Changingstate.user.namemutates a nested property but does not replace theuserobject, so the watch callback does not run.Final Answer:
The watch callback runs only ifstate.useris replaced entirely -> Option AQuick Check:
shallowReactive watches top-level refs only [OK]
- Expecting watch to detect nested property changes
- Assuming watch runs on every nested mutation
- Confusing shallowReactive with deep reactive
Ready to prove it?
Pass the test and this pattern moves unknown
