0
0
Vueframework~20 mins

Reactive data with ref in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Ref Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this Vue component?
Consider this Vue 3 component using ref to track a counter. What will be displayed when the button is clicked twice?
Vue
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
  count.value++
}
</script>
<template>
  <div>
    <p>Count is: {{ count }}</p>
    <button @click="increment">Add</button>
  </div>
</template>
ACount is: 2
BCount is: 0
CCount is: [object Object]
DCount is: undefined
Attempts:
2 left
💡 Hint
Remember that ref creates a reactive object and you access its value with .value inside script, but in template you can use it directly.
state_output
intermediate
1:30remaining
What is the value of message after running this code?
Given this Vue 3 setup script, what is the value of message.value after the code runs?
Vue
<script setup>
import { ref } from 'vue'
const message = ref('Hello')
message.value = message.value + ' World'
</script>
A'Hello World'
B'HelloWorld'
C'Hello'
Dundefined
Attempts:
2 left
💡 Hint
Check how string concatenation with a space works.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a reactive reference to an object?
You want to create a reactive reference to an object with a name property set to 'Alice'. Which code is correct?
Aconst user = ref['name' => 'Alice']
Bconst user = ref({ name: 'Alice' })
Cconst user = ref(name: 'Alice')
Dconst user = ref('name' = 'Alice')
Attempts:
2 left
💡 Hint
Remember the syntax for creating an object in JavaScript.
🔧 Debug
advanced
2:30remaining
Why does this Vue component not update the displayed count?
This Vue 3 component uses a ref for count but the displayed number never changes when clicking the button. What is the cause?
Vue
<script setup>
import { ref } from 'vue'
const count = 0
function increment() {
  count++
}
</script>
<template>
  <div>
    <p>Count: {{ count }}</p>
    <button @click="increment">Add</button>
  </div>
</template>
AThe template syntax {{ count }} is invalid
BThe increment function is missing parentheses
Ccount is not a ref, so Vue does not track changes to it
DThe button click event is not bound correctly
Attempts:
2 left
💡 Hint
Check how reactivity works with refs in Vue.
🧠 Conceptual
expert
3:00remaining
What happens when you destructure a ref object directly in Vue 3?
Given const count = ref(5), what is the value of num after const { value: num } = count and what is the reactivity behavior?
Vue
import { ref } from 'vue'
const count = ref(5)
const { value: num } = count
num = 10
Anum is undefined and causes an error
Bnum is a reactive ref linked to count.value
Cnum updates count.value automatically when changed
Dnum is 10, but changing num does not update count.value
Attempts:
2 left
💡 Hint
Destructuring extracts the value but does not keep reactivity.