0
0
Vueframework~20 mins

toRef and toRefs for destructuring in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Reactivity Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will be the output when using toRef for a single property?
Consider the following Vue 3 component setup using toRef. What will be displayed when the button is clicked twice?
Vue
<script setup>
import { reactive, toRef } from 'vue'

const state = reactive({ count: 0 })
const countRef = toRef(state, 'count')

function increment() {
  countRef.value++
}
</script>
<template>
  <div>
    <p>Count: {{ countRef.value }}</p>
    <button @click="increment">Increment</button>
  </div>
</template>
ACount: 0
BCount: undefined
CCount: 1
DCount: 2
Attempts:
2 left
💡 Hint
Remember that toRef creates a reactive reference to the original property.
state_output
intermediate
1:30remaining
What is the value of nameRef.value after modifying the original object?
Given the following code, what will be the value of nameRef.value after user.name is changed?
Vue
<script setup>
import { reactive, toRef } from 'vue'

const user = reactive({ name: 'Alice', age: 30 })
const nameRef = toRef(user, 'name')

user.name = 'Bob'
</script>
Anull
B'Bob'
Cundefined
D'Alice'
Attempts:
2 left
💡 Hint
Think about how toRef keeps the reference reactive to the original property.
📝 Syntax
advanced
1:30remaining
Which option correctly uses toRefs to destructure a reactive object?
You want to destructure all properties of a reactive object profile into refs. Which code snippet is correct?
Vue
const profile = reactive({ firstName: 'John', lastName: 'Doe' })
Aconst { firstName, lastName } = toRefs(profile)
Bconst { firstName, lastName } = reactive(profile)
Cconst [firstName, lastName] = toRefs(profile)
Dconst [firstName, lastName] = reactive(profile)
Attempts:
2 left
💡 Hint
toRefs returns an object with refs for each property.
🔧 Debug
advanced
2:00remaining
Why is ageRef.value undefined when using toRef?
Examine the code below. Why does console.log(ageRef.value) output undefined?
Vue
<script setup>
import { reactive, toRef } from 'vue'

const person = reactive({ name: 'Eve' })
const ageRef = toRef(person, 'age')
console.log(ageRef.value)
</script>
ABecause 'age' does not exist on the reactive object, so <code>toRef</code> creates an undefined ref.
BBecause <code>toRef</code> only works with arrays, not objects.
CBecause <code>toRef</code> requires a third argument to specify default values.
DBecause <code>reactive</code> objects cannot be destructured with <code>toRef</code>.
Attempts:
2 left
💡 Hint
Check if the property exists on the reactive object before creating a ref.
🧠 Conceptual
expert
2:30remaining
What is the main difference between toRef and toRefs in Vue 3?
Choose the option that best explains the difference between toRef and toRefs.
A<code>toRef</code> is used only for arrays, and <code>toRefs</code> is used only for objects.
B<code>toRef</code> converts a reactive object into a ref, while <code>toRefs</code> converts a ref into a reactive object.
C<code>toRef</code> creates a ref for a single property of a reactive object, while <code>toRefs</code> creates refs for all properties of a reactive object and returns them as an object.
D<code>toRef</code> creates a deep reactive copy of a property, while <code>toRefs</code> creates shallow copies of all properties.
Attempts:
2 left
💡 Hint
Think about how many properties each function handles and what they return.