In Vue 3, composables are functions that encapsulate reusable logic. Why is it recommended to prefix composable function names, for example, with use?
Think about how naming helps developers understand code and avoid mistakes.
Using a prefix like use helps developers recognize composables easily and prevents name clashes with other functions or variables in the codebase.
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?
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?
Each call to increment() increases the count by one.
The count starts at 0 and increments twice, so its value becomes 2.
Which option correctly defines and exports a composable named with the recommended prefix?
Look for the function name starting with use and proper export syntax.
Option A correctly uses the use prefix and exports the function directly. Other options either lack the prefix or use incorrect naming conventions.
Given two composables in the same project, one named fetchData and another named useFetchData, importing both in a component causes a conflict error. Why?
Think about how import names and variable names in JavaScript can clash.
Importing two functions with similar names without aliasing can cause conflicts if they are imported with the same local name. The prefix helps but does not prevent conflicts if import names collide.
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?
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?
Consider that the reactive variable is shared outside the composable function.
Because sharedCount is declared outside the composable, both components share the same reactive state. Each increment adds 1, so total is 2.