0
0
Vueframework~20 mins

Composables for reusable logic in Vue - Practice Problems & Coding Challenges

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!
component_behavior
intermediate
2:00remaining
What is the output of this Vue composable usage?

Consider this composable and component using it:

import { ref } from 'vue';

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

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

What is the value of count.value after these calls?

A0
B1
C2
Dundefined
Attempts:
2 left
💡 Hint

Each call to increment() increases count.value by 1.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a composable that returns a reactive string?

Choose the correct composable function that returns a reactive string message initialized to "Hello".

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

Use ref for primitive reactive values like strings.

🔧 Debug
advanced
2:00remaining
Why does this composable cause shared state across components?

Look at this composable:

import { ref } from 'vue';

const count = ref(0);

export function useSharedCounter() {
  function increment() {
    count.value++;
  }
  return { count, increment };
}

When used in multiple components, why do they share the same count value?

ABecause Vue automatically shares all composable state between components.
BBecause <code>count</code> is declared outside the function, so it is shared across all calls.
CBecause the function does not return <code>count</code> properly.
DBecause <code>ref</code> creates a global reactive variable by default.
Attempts:
2 left
💡 Hint

Think about where variables are declared in JavaScript modules.

state_output
advanced
2:00remaining
What is the output of this composable with watchEffect?

Given this composable and usage:

import { ref, watchEffect } from 'vue';

export function useDoubleCounter() {
  const count = ref(1);
  const double = ref(0);

  watchEffect(() => {
    double.value = count.value * 2;
  });

  function increment() {
    count.value++;
  }

  return { count, double, increment };
}

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

What are the final values of count.value and double.value?

A<code>count.value</code> is 3 and <code>double.value</code> is 6
B<code>count.value</code> is 2 and <code>double.value</code> is 4
C<code>count.value</code> is 3 and <code>double.value</code> is 3
D<code>count.value</code> is 1 and <code>double.value</code> is 2
Attempts:
2 left
💡 Hint

watchEffect updates double whenever count changes.

🧠 Conceptual
expert
2:00remaining
Why use composables instead of mixins for reusable logic?

Which reason best explains why Vue composables are preferred over mixins for sharing reusable logic?

AComposables provide better type inference and avoid naming conflicts by using explicit function calls.
BMixins are faster to run but harder to write.
CComposables automatically merge data and methods without conflicts.
DMixins allow better control over lifecycle hooks than composables.
Attempts:
2 left
💡 Hint

Think about clarity and code safety when reusing logic.