0
0
Vueframework~20 mins

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

Choose your learning style9 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.