Challenge - 5 Problems
Vue Composable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this custom composable return?
Consider this Vue 3 custom composable that tracks window width. What is the output when you call
useWindowWidth()?Vue
import { ref, onMounted, onUnmounted } from 'vue'; export function useWindowWidth() { const width = ref(window.innerWidth); function updateWidth() { width.value = window.innerWidth; } onMounted(() => { window.addEventListener('resize', updateWidth); }); onUnmounted(() => { window.removeEventListener('resize', updateWidth); }); return { width }; }
Attempts:
2 left
💡 Hint
Think about what
ref returns and how the event listeners affect it.✗ Incorrect
The composable returns an object with a reactive width property. This property updates automatically when the window is resized because of the event listener.
❓ state_output
intermediate1:30remaining
What is the value of
count.value after calling useCounter() and incrementing twice?Given this custom composable for a counter, what is the value of
count.value after calling increment() twice?Vue
import { ref } from 'vue'; export function useCounter() { const count = ref(0); function increment() { count.value++; } return { count, increment }; } // Usage example: const { count, increment } = useCounter(); increment(); increment();
Attempts:
2 left
💡 Hint
Remember that
count is a ref and increments by 1 each time increment() is called.✗ Incorrect
The count starts at 0 and increments by 1 each time increment() is called. After two calls, it becomes 2.
📝 Syntax
advanced2:30remaining
Which option correctly defines a composable that returns a reactive boolean toggled by a function?
Select the option that correctly defines a Vue 3 composable returning a reactive boolean
isActive and a function toggle to flip its value.Attempts:
2 left
💡 Hint
Remember to use
.value to access or update refs.✗ Incorrect
Option A correctly uses ref and updates isActive.value. Option A returns a non-reactive primitive. Option A uses a plain variable, not reactive. Option A tries to toggle the ref itself, not its value.
🔧 Debug
advanced2:00remaining
Why does this composable not update the reactive property on button click?
This composable is intended to toggle a boolean on button click, but the UI does not update. What is the cause?
Vue
<template> <button @click="toggle">Toggle</button> <p>{{ isActive }}</p> </template> <script setup> import { ref } from 'vue'; const isActive = ref(false); function toggle() { isActive.value = !isActive.value; } </script>
Attempts:
2 left
💡 Hint
Check how you update reactive refs inside functions.
✗ Incorrect
The toggle function tries to assign a boolean directly to the ref variable isActive, which breaks reactivity. It should update isActive.value instead.
🧠 Conceptual
expert1:30remaining
What is the main advantage of using a custom composable in Vue 3?
Why do developers create custom composables in Vue 3? Choose the best explanation.
Attempts:
2 left
💡 Hint
Think about code reuse and organization in Vue 3.
✗ Incorrect
Custom composables let you share reactive logic easily between components, improving code reuse and clarity.