0
0
Vueframework~20 mins

Creating a custom composable in Vue - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Composable Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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 };
}
AA function that returns the current window width when called
BA number representing the window width at the time of calling
CAn object with a reactive property 'width' that updates on window resize
DAn array containing the window width and a function to update it
Attempts:
2 left
💡 Hint
Think about what ref returns and how the event listeners affect it.
state_output
intermediate
1: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();
A0
Bundefined
C1
D2
Attempts:
2 left
💡 Hint
Remember that count is a ref and increments by 1 each time increment() is called.
📝 Syntax
advanced
2: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.
A
import { ref } from 'vue';
export function useToggle() {
  const isActive = ref(false);
  function toggle() {
    isActive.value = !isActive.value;
  }
  return { isActive, toggle };
}
B
import { ref } from 'vue';
export function useToggle() {
  let isActive = false;
  function toggle() {
    isActive = !isActive;
  }
  return { isActive, toggle };
}
C
import { reactive } from 'vue';
export function useToggle() {
  const state = reactive({ isActive: false });
  function toggle() {
    state.isActive = !state.isActive;
  }
  return { isActive: state.isActive, toggle };
}
D
import { ref } from 'vue';
export function useToggle() {
  const isActive = ref(false);
  function toggle() {
    isActive = !isActive;
  }
  return { isActive, toggle };
}
Attempts:
2 left
💡 Hint
Remember to use .value to access or update refs.
🔧 Debug
advanced
2: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>
AThe ref is not imported from 'vue'
BThe toggle function assigns a new value to the ref variable instead of updating its .value property
CThe button click event is not bound correctly
DThe template does not use double curly braces to display isActive
Attempts:
2 left
💡 Hint
Check how you update reactive refs inside functions.
🧠 Conceptual
expert
1: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.
ATo reuse reactive logic across multiple components without repeating code
BTo replace Vue components with simpler functions
CTo automatically generate UI templates from JavaScript code
DTo avoid using the Composition API and stick to Options API
Attempts:
2 left
💡 Hint
Think about code reuse and organization in Vue 3.