Using prefixes in composable names helps keep your code clear and organized. It shows what the composable does and avoids name clashes.
Composable naming conventions (use prefix) in Vue
function usePrefixFeatureName() { // composable logic here return { /* reactive data or methods */ }; }
Start composable names with use to follow Vue community conventions.
Add a clear prefix that groups related composables, like useAuth or useCart.
function useAuthLogin() { // logic for login return { loginUser }; }
function useCartItems() { // logic for cart items return { items }; }
function useUserProfile() { // logic for user profile return { profile }; }
This example shows a composable named useCounter that manages a count state. The prefix use indicates it is a composable. The component uses this composable to display and increment the count.
import { ref } from 'vue'; // Composable with prefix 'useCounter' export function useCounter() { const count = ref(0); function increment() { count.value++; } return { count, increment }; } // Vue component using the composable import { defineComponent } from 'vue'; import { useCounter } from './useCounter'; export default defineComponent({ setup() { const { count, increment } = useCounter(); return { count, increment }; }, template: `<button @click="increment">Count: {{ count }}</button>` });
Always use the use prefix for composables to follow Vue style.
Choose prefixes that describe the feature or domain clearly.
Keep composable names short but descriptive to improve readability.
Composable names should start with use to show they are composables.
Use prefixes to group related composables and avoid confusion.
Clear naming helps you and others understand your code faster.
Practice
use?Solution
Step 1: Understand Vue composable naming conventions
Vue recommends starting composable function names withuseto clearly identify them as composables.Step 2: Recognize the purpose of the prefix
The prefix helps developers quickly know the function is a composable and follows community standards.Final Answer:
To clearly indicate they are composables and follow Vue conventions -> Option CQuick Check:
Composable prefix = use [OK]
- Thinking prefix affects performance
- Believing prefix hides the function
- Ignoring naming conventions
Solution
Step 1: Identify the composable prefix
The composable should start withuseto follow Vue naming conventions.Step 2: Check the full function name
useFetchUserDataclearly shows it is a composable fetching user data.Final Answer:
useFetchUserData() -> Option AQuick Check:
Composable name starts with 'use' [OK]
- Omitting the 'use' prefix
- Using verbs without 'use'
- Confusing composable with normal functions
export function useCounter() {
const count = ref(0);
function increment() { count.value++ }
return { count, increment };
}What will be the output of
console.log(counter.count.value) after calling counter.increment() twice?Solution
Step 1: Understand the initial state
Thecountref starts at 0.Step 2: Apply the increment function twice
Each call toincrement()increasescount.valueby 1, so after two calls,count.valueis 2.Final Answer:
2 -> Option BQuick Check:
Increment twice = count 2 [OK]
- Forgetting to access .value of ref
- Assuming count resets
- Confusing function call effects
export function fetchData() {
// composable logic
}Solution
Step 1: Check naming conventions for composables
Composable functions must start withuseto be recognized as such.Step 2: Identify the missing prefix
The functionfetchDatalacks theuseprefix, so it breaks the convention.Final Answer:
Missing 'use' prefix in function name -> Option DQuick Check:
Composable names need 'use' prefix [OK]
- Ignoring naming conventions
- Thinking export is disallowed
- Confusing function types
useAuthLogin, useAuthLogout, and useAuthStatus. Why is using the useAuth prefix a good practice?Solution
Step 1: Understand prefix grouping purpose
Using a common prefix groups related composables logically.Step 2: Recognize benefits of grouping
This grouping helps developers quickly find and understand related functions, improving maintainability.Final Answer:
It groups related composables, making code easier to understand and maintain -> Option AQuick Check:
Prefix groups related composables [OK]
- Assuming prefix affects performance
- Thinking prefix hides composables
- Believing prefix auto-imports functions
Ready to prove it?
Pass the test and this pattern moves unknown
