0
0
Vueframework~20 mins

Composable naming conventions (use prefix) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Composable Naming Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Why use a prefix in Vue composable names?

In Vue 3, composables are functions that encapsulate reusable logic. Why is it recommended to prefix composable function names, for example, with use?

ATo clearly indicate the function is a composable and to avoid naming conflicts with other functions or variables.
BTo make the function run automatically when the component mounts.
CTo ensure the function is only used inside the <code>setup()</code> method of a component.
DTo make the function globally available without importing it.
Attempts:
2 left
💡 Hint

Think about how naming helps developers understand code and avoid mistakes.

component_behavior
intermediate
1:30remaining
What is the output when using a composable with prefix?

Consider this Vue 3 composable function named useCounter that returns a reactive count and an increment function. What will be 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 };
}

// In a component setup:
const { count, increment } = useCounter();
increment();
increment();
// What is count.value now?
A0
B2
C1
Dundefined
Attempts:
2 left
💡 Hint

Each call to increment() increases the count by one.

📝 Syntax
advanced
2:00remaining
Identify the correct composable naming and export syntax

Which option correctly defines and exports a composable named with the recommended prefix?

Aexport function useFetchData() { /* logic */ }
Bfunction fetchDataUse() { /* logic */ } export default fetchDataUse;
Cexport const fetchData = () => { /* logic */ }
Dexport function FetchData() { /* logic */ }
Attempts:
2 left
💡 Hint

Look for the function name starting with use and proper export syntax.

🔧 Debug
advanced
2:00remaining
Why does this composable cause a naming conflict?

Given two composables in the same project, one named fetchData and another named useFetchData, importing both in a component causes a conflict error. Why?

ABecause Vue does not allow more than one composable in a component.
BBecause composables must always be default exports to avoid conflicts.
CBecause the <code>use</code> prefix is reserved and cannot be used in multiple composables.
DBecause <code>fetchData</code> and <code>useFetchData</code> are too similar and the import aliases overlap causing a naming conflict.
Attempts:
2 left
💡 Hint

Think about how import names and variable names in JavaScript can clash.

state_output
expert
2:30remaining
What is the final value of shared state in composables with prefix?

Two components import the same composable useSharedState which uses a reactive variable declared outside the function. What is the value of sharedCount.value after both components call increment() once?

Vue
import { ref } from 'vue';

const sharedCount = ref(0);

export function useSharedState() {
  function increment() {
    sharedCount.value++;
  }
  return { sharedCount, increment };
}

// Component A:
const { sharedCount, increment } = useSharedState();
increment();

// Component B:
const { sharedCount: sharedCountB, increment: incrementB } = useSharedState();
incrementB();

// What is sharedCount.value now?
A1
B0
C2
Dundefined
Attempts:
2 left
💡 Hint

Consider that the reactive variable is shared outside the composable function.