0
0
Vueframework~20 mins

Why composables matter in Vue - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composable Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use composables in Vue 3?

What is the main benefit of using composables in Vue 3's Composition API?

AThey automatically optimize rendering performance without extra code.
BThey allow reusing logic across components without duplicating code.
CThey replace templates with plain JavaScript functions for UI rendering.
DThey enforce strict typing on component props and events.
Attempts:
2 left
💡 Hint

Think about how you can share code between components easily.

component_behavior
intermediate
2:00remaining
Effect of composable on component state

Given this composable and component code, what will be the value of count after clicking the button twice?

Vue
import { ref } from 'vue';

export function useCounter() {
  const count = ref(0);
  function increment() {
    count.value++;
  }
  return { count, increment };
}

// In component setup:
const { count, increment } = useCounter();

// Button click calls increment()
A2
B0
C1
Dundefined
Attempts:
2 left
💡 Hint

Each click calls increment() which increases count by 1.

lifecycle
advanced
2:00remaining
Composables and component lifecycle hooks

Which lifecycle hook is best to use inside a composable to run code when the component using it is mounted?

Asetup
Bcreated
CbeforeDestroy
DonMounted
Attempts:
2 left
💡 Hint

Consider when the DOM is ready for interaction.

📝 Syntax
advanced
2:00remaining
Correct syntax for a composable function

Which option shows the correct syntax for a Vue 3 composable that provides a reactive message and a function to update it?

A
import { ref } from 'vue';
export function useMessage() {
  const message = ref('Hello');
  function updateMessage(newMsg) {
    message.value = newMsg;
  }
  return { message, updateMessage };
}
B
import { reactive } from 'vue';
export function useMessage() {
  const message = reactive('Hello');
  function updateMessage(newMsg) {
    message = newMsg;
  }
  return { message, updateMessage };
}
C
export function useMessage() {
  let message = 'Hello';
  function updateMessage(newMsg) {
    message = newMsg;
  }
  return { message, updateMessage };
}
D
import { ref } from 'vue';
export function useMessage() {
  const message = ref('Hello');
  function updateMessage(newMsg) {
    message = newMsg;
  }
  return { message, updateMessage };
}
Attempts:
2 left
💡 Hint

Remember how to update a ref value properly.

🔧 Debug
expert
3:00remaining
Why does this composable cause shared state bug?

Consider this composable used in multiple components. Why do all components share the same count value?

import { ref } from 'vue';
const count = ref(0);
export function useSharedCounter() {
  function increment() {
    count.value++;
  }
  return { count, increment };
}
ABecause <code>increment</code> does not update <code>count.value</code> correctly.
BBecause <code>ref</code> does not create reactive state inside composables.
CBecause <code>count</code> is declared outside the function, so it's shared across all uses.
DBecause the composable is missing a return statement.
Attempts:
2 left
💡 Hint

Think about where variables are declared and how that affects sharing.