Bird
Raised Fist0
Vueframework~20 mins

Shallow ref and shallow reactive in Vue - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Shallow Reactivity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What happens when updating a nested property in a shallow reactive object?

Consider this Vue 3 code using shallowReactive:

const state = shallowReactive({ user: { name: 'Alice', age: 30 } })

state.user.name = 'Bob'

What will Vue track and update reactively?

AOnly the top-level <code>user</code> property is reactive; nested changes like <code>name</code> are not tracked.
BBoth top-level and nested properties like <code>name</code> are fully reactive and tracked.
CNo properties are reactive; <code>shallowReactive</code> disables all reactivity.
DOnly nested properties like <code>name</code> are reactive; the top-level <code>user</code> is not.
Attempts:
2 left
💡 Hint

Think about what shallow means in this context.

❓ state_output
intermediate
2:00remaining
What is the output after modifying a shallow ref's nested object?

Given this Vue 3 code:

const user = shallowRef({ name: 'Alice', age: 30 })
user.value.name = 'Bob'
console.log(user.value.name)

What will be logged?

A'Bob'
B'Alice'
Cundefined
DThrows a runtime error
Attempts:
2 left
💡 Hint

Consider how shallowRef treats the wrapped object and its nested properties.

📝 Syntax
advanced
2:00remaining
Which code correctly creates a shallow reactive object with a nested array?

Choose the correct Vue 3 code snippet that creates a shallow reactive object with a nested array, so only the top-level object is reactive but the array is not deeply reactive.

Aconst data = reactive({ items: [1, 2, 3] })
Bconst data = shallowRef({ items: reactive([1, 2, 3]) })
Cconst data = shallowReactive({ items: [1, 2, 3] })
Dconst data = reactive({ items: shallowRef([1, 2, 3]) })
Attempts:
2 left
💡 Hint

Remember that shallowReactive only makes the top-level reactive.

🔧 Debug
advanced
2:00remaining
Why does this shallowRef nested change not trigger updates?

Look at this Vue 3 code:

const state = shallowRef({ count: 0, nested: { value: 10 } })

function increment() {
  state.value.nested.value++
}

Why might Vue not update the UI when increment() is called?

ABecause Vue 3 does not support reactivity on objects inside refs.
BBecause <code>nested</code> is not initialized as a reactive object.
CBecause <code>increment</code> function is missing a call to <code>state.value = {...state.value}</code> to trigger reactivity.
DBecause <code>shallowRef</code> only tracks changes to the reference itself, not nested properties.
Attempts:
2 left
💡 Hint

Think about what triggers Vue's reactivity system for refs.

🧠 Conceptual
expert
3:00remaining
Which statement best describes the difference between shallowRef and shallowReactive?

Choose the most accurate description of how shallowRef and shallowReactive differ in Vue 3 reactivity.

A<code>shallowRef</code> deeply tracks nested properties; <code>shallowReactive</code> only tracks the reference itself.
B<code>shallowRef</code> creates a reactive reference to a value without making nested properties reactive; <code>shallowReactive</code> creates a reactive proxy for an object but only tracks top-level properties, not nested ones.
C<code>shallowRef</code> and <code>shallowReactive</code> behave identically and can be used interchangeably.
D<code>shallowReactive</code> creates a reactive reference; <code>shallowRef</code> creates a reactive proxy object.
Attempts:
2 left
💡 Hint

Focus on what each function returns and how deep their reactivity goes.

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

  1. Step 1: Understand shallowRef behavior

    shallowRef tracks reactivity only when the reference changes, not inside nested objects.
  2. Step 2: Compare with deep reactivity

    Unlike ref or reactive, it does not track nested property changes.
  3. Final Answer:

    Only changes to the reference itself, not nested properties -> Option C
  4. Quick Check:

    shallowRef tracks reference, not deep changes [OK]
Hint: shallowRef tracks only the outer reference change [OK]
Common Mistakes:
  • Thinking shallowRef tracks nested property changes
  • Confusing shallowRef with reactive
  • Assuming shallowRef tracks primitive values inside
2. Which of the following is the correct way to create a shallow reactive object in Vue?
easy
A. const state = reactive({ count: 0, nested: { value: 1 } })
B. const state = shallowReactive({ count: 0, nested: { value: 1 } })
C. const state = shallowRef({ count: 0, nested: { value: 1 } })
D. const state = ref({ count: 0, nested: { value: 1 } })

Solution

  1. Step 1: Identify shallow reactive syntax

    shallowReactive is used to create a shallow reactive object that tracks only top-level properties.
  2. Step 2: Differentiate from other APIs

    reactive creates deep reactivity, shallowRef and ref create refs, not shallow reactive objects.
  3. Final Answer:

    const state = shallowReactive({ count: 0, nested: { value: 1 } }) -> Option B
  4. Quick Check:

    shallowReactive creates shallow reactive objects [OK]
Hint: Use shallowReactive() for shallow reactive objects [OK]
Common Mistakes:
  • Using reactive() instead of shallowReactive()
  • Confusing shallowRef with shallowReactive
  • Using ref() for reactive objects
3. Given the code:
const state = shallowReactive({ count: 0, nested: { value: 1 } });

state.nested.value = 2;
console.log(state.nested.value);

What will be logged to the console?
medium
A. Error: Cannot assign to nested property
B. 1
C. undefined
D. 2

Solution

  1. Step 1: Understand shallowReactive behavior

    shallowReactive tracks only top-level properties reactively, but nested objects still exist and can be mutated normally.
  2. Step 2: Check nested property assignment

    Assigning state.nested.value = 2 changes the nested object property directly, so the value updates.
  3. Final Answer:

    2 -> Option D
  4. Quick Check:

    Nested objects update but are not reactive [OK]
Hint: shallowReactive allows nested mutation but no reactivity inside [OK]
Common Mistakes:
  • Assuming nested changes are not possible
  • Expecting nested changes to trigger reactivity
  • Thinking assignment causes error
4. What is the problem with this code snippet?
const state = shallowRef({ count: 0, nested: { value: 1 } });

state.value.nested.value = 5;
console.log(state.value.nested.value);
medium
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

  1. Step 1: Analyze shallowRef behavior

    shallowRef tracks only changes to the reference itself, not nested properties inside the object.
  2. Step 2: Understand nested mutation effect

    Changing state.value.nested.value updates the nested property but does not trigger Vue reactivity updates.
  3. Final Answer:

    Nested property change won't trigger reactivity -> Option A
  4. 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?
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?
hard
A. The watch callback runs only if state.user is replaced entirely
B. The watch callback runs immediately when state.user.name changes
C. The watch callback will run when state.user.name changes
D. The watch callback runs twice for nested changes

Solution

  1. Step 1: Understand shallowReactive and watch

    shallowReactive tracks only top-level properties reactively. The watch on state.user triggers only if the user object reference changes.
  2. Step 2: Analyze nested property mutation

    Changing state.user.name mutates a nested property but does not replace the user object, so the watch callback does not run.
  3. Final Answer:

    The watch callback runs only if state.user is replaced entirely -> Option A
  4. Quick Check:

    shallowReactive watches top-level refs only [OK]
Hint: Watch triggers only on reference changes with shallowReactive [OK]
Common Mistakes:
  • Expecting watch to detect nested property changes
  • Assuming watch runs on every nested mutation
  • Confusing shallowReactive with deep reactive
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard