0
0
Vueframework~20 mins

Ref and reactive in Composition API in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Composition API Ref & Reactive Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Vue component's template?

Consider this Vue 3 component using the Composition API. What will be displayed in the browser?

Vue
<script setup>
import { ref, reactive } from 'vue'
const count = ref(0)
const state = reactive({ clicks: 0 })
function increment() {
  count.value++
  state.clicks++
}
</script>
<template>
  <button @click="increment">Click me</button>
  <p>Count: {{ count }}</p>
  <p>Clicks: {{ state.clicks }}</p>
</template>
ACount and clicks do not update on click because reactive is not used correctly
BCount: 0\nClicks: 0 initially, then both increment by 1 on each click
CCount: 0\nClicks: 0 initially, but only count increments on click, clicks stays 0
DCount: 0\nClicks: 0 initially, but only clicks increments on click, count stays 0
Attempts:
2 left
💡 Hint

Remember that ref values update with .value and reactive objects update their properties directly.

📝 Syntax
intermediate
2:00remaining
Which option correctly creates a reactive object with nested properties?

Which code snippet correctly creates a reactive object with nested properties that Vue tracks reactively?

Aconst state = reactive({ user: ref({ name: 'Alice', age: 30 }) })
Bconst state = ref({ user: { name: 'Alice', age: 30 } })
Cconst state = reactive({ user: { name: 'Alice', age: 30 } })
Dconst state = ref({ user: reactive({ name: 'Alice', age: 30 }) })
Attempts:
2 left
💡 Hint

Think about how reactive and ref handle nested objects.

🔧 Debug
advanced
2:00remaining
Why does this reactive state not update the template?

Given this Vue component, why does the template not update when state.count changes?

Vue
<script setup>
import { reactive } from 'vue'
const state = reactive({ count: 0 })
function increment() {
  state.count = state.count + 1
}
</script>
<template>
  <button @click="increment">Add</button>
  <p>{{ state.count }}</p>
</template>
AThe template does update; the code is correct and reactive works as expected.
BThe reactive object was not declared properly; it should use ref instead.
CThe increment function does not update state.count correctly; it should use state.count.value.
DThe template must unwrap reactive properties manually using .value.
Attempts:
2 left
💡 Hint

Check how reactive objects and their properties are accessed and updated.

state_output
advanced
2:00remaining
What is the value of count after this code runs?

Given this code snippet, what is the final value of count.value?

Vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function update() {
  count.value += 1
  count.value = count.value * 2
}
update()
update()
</script>
A8
B2
C4
D6
Attempts:
2 left
💡 Hint

Calculate step by step what happens to count.value after each update() call.

🧠 Conceptual
expert
2:00remaining
Which statement about ref and reactive is true?

Choose the correct statement about ref and reactive in Vue's Composition API.

A<code>ref</code> creates a reactive reference to a primitive or object, while <code>reactive</code> creates a reactive proxy for an object only.
B<code>reactive</code> can be used with primitive values directly, while <code>ref</code> only works with objects.
C<code>ref</code> and <code>reactive</code> are interchangeable and produce identical reactivity behavior.
D<code>ref</code> automatically unwraps nested objects deeply, while <code>reactive</code> only unwraps shallowly.
Attempts:
2 left
💡 Hint

Think about what types each function is designed to handle.