0
0
Vueframework~10 mins

Shallow ref and shallow reactive in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Shallow ref and shallow reactive
Create shallowRef or shallowReactive
Wrap object shallowly
Access or modify top-level properties
Reactivity triggers only for top-level changes
Nested objects remain non-reactive
Use in template or watch for updates
This flow shows how shallowRef or shallowReactive wraps an object shallowly, tracking only top-level changes reactively while nested objects stay non-reactive.
Execution Sample
Vue
import { shallowRef, shallowReactive } from 'vue';

const state = shallowReactive({
  user: { name: 'Alice' },
  count: 0
});

state.count = 1; // triggers update
state.user.name = 'Bob'; // does NOT trigger update
This code creates a shallow reactive object where only top-level changes like count update reactively, but nested changes like user.name do not.
Execution Table
StepActionProperty ChangedReactivity TriggeredEffect
1Create shallowReactive objectnonenonestate is reactive only at top level
2Change state.count from 0 to 1countYesReactive update triggered
3Change state.user.name from 'Alice' to 'Bob'user.nameNoNo reactive update triggered
4Replace state.user with new objectuserYesReactive update triggered
5Modify nested property of new state.useruser.nameNoNo reactive update triggered
💡 No more changes; nested properties do not trigger reactivity in shallowReactive
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
state.count01111
state.user.name'Alice''Alice''Bob''Bob''Bob'
Key Moments - 2 Insights
Why does changing state.user.name not trigger a reactive update?
Because shallowReactive only tracks top-level properties reactively. Nested properties like user.name are not reactive, as shown in step 3 of the execution_table.
What happens if we replace the entire nested object state.user?
Replacing the whole nested object triggers reactivity because the top-level property user changes, as shown in step 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step does changing a nested property NOT trigger reactivity?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Check the 'Reactivity Triggered' column for steps 3 and 5 in the execution_table.
According to variable_tracker, what is the value of state.count after step 2?
A1
B'Alice'
C0
D'Bob'
💡 Hint
Look at the 'state.count' row under 'After Step 2' in variable_tracker.
If we want nested properties to be reactive, what should we use instead of shallowReactive?
Aref
BshallowRef
Creactive
Dcomputed
💡 Hint
Recall that shallowReactive only tracks top-level; full reactive tracks nested properties.
Concept Snapshot
shallowRef and shallowReactive wrap objects shallowly.
Only top-level properties are reactive.
Nested objects inside remain non-reactive.
Changing nested properties does NOT trigger updates.
Replacing top-level nested objects triggers reactivity.
Use reactive for deep reactivity.
Full Transcript
This lesson shows how Vue's shallowRef and shallowReactive work by tracking only top-level changes reactively. When you create a shallowReactive object, changes to its direct properties trigger updates, but changes inside nested objects do not. For example, changing state.count triggers reactivity, but changing state.user.name does not. However, if you replace the entire nested object state.user, that triggers reactivity because the top-level property changed. This helps optimize performance when you don't need deep reactivity. To track nested changes reactively, use reactive instead of shallowReactive.