📖 Scenario: You are building a Vue 3 application that uses composables to organize reusable logic. To keep your code clear and avoid naming conflicts, you want to follow a naming convention by adding a prefix to your composable functions.
🎯 Goal: Create a composable function with a prefix use following Vue 3 Composition API naming conventions. Then use it inside a Vue component.
📋 What You'll Learn
Create a composable function named useCounter that returns a reactive count and an increment function.
Create a Vue component that imports and uses useCounter.
Display the count and a button to increment it in the component template.
Use the setup function and ref from Vue Composition API.
💡 Why This Matters
🌍 Real World
Using composables with clear naming conventions helps keep Vue projects organized and easy to maintain, especially when sharing logic across components.
💼 Career
Understanding composable naming and usage is essential for Vue developers to write clean, reusable code and collaborate effectively in teams.
Progress0 / 4 steps
1
Create the composable function useCounter
Create a composable function named useCounter that uses Vue's ref to create a reactive variable count initialized to 0. Also create a function increment that increases count by 1. Return both count and increment from useCounter.
Vue
Hint
Use ref(0) to create a reactive count variable. Define increment to add 1 to count.value. Return both in an object.
2
Import useCounter in a Vue component
In a Vue component file, import the composable function useCounter from its module. Then create a setup function where you call useCounter and get count and increment.
Vue
Hint
Import useCounter and call it inside setup(). Destructure count and increment from the result and return them.
3
Add template to display count and increment button
Add a template section to the Vue component that shows the current count value and a button. When the button is clicked, it should call the increment function to increase the count.
Vue
Hint
Use {{ count }} to show the count. Add a button with @click="increment" to call the increment function.
4
Complete the Vue component with script setup syntax
Convert the Vue component to use the <script setup> syntax. Import useCounter and call it directly inside <script setup>. Make sure the template uses the reactive count and the increment function.
Vue
Hint
Use <script setup> to simplify the component. Import and call useCounter directly.
Practice
(1/5)
1. Why should composable functions in Vue start with the prefix use?
easy
A. To make them private and inaccessible outside the file
B. To make them run faster in the browser
C. To clearly indicate they are composables and follow Vue conventions
D. To prevent them from being imported accidentally
Vue recommends starting composable function names with use to 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 C
Quick Check:
Composable prefix = use [OK]
Hint: Composable names start with 'use' to show their purpose [OK]
Common Mistakes:
Thinking prefix affects performance
Believing prefix hides the function
Ignoring naming conventions
2. Which of the following is the correct syntax for naming a composable that fetches user data?
easy
A. useFetchUserData()
B. fetchUserData()
C. getUserData()
D. userDataFetch()
Solution
Step 1: Identify the composable prefix
The composable should start with use to follow Vue naming conventions.
Step 2: Check the full function name
useFetchUserData clearly shows it is a composable fetching user data.
Final Answer:
useFetchUserData() -> Option A
Quick Check:
Composable name starts with 'use' [OK]
Hint: Composable names always start with 'use' [OK]
Common Mistakes:
Omitting the 'use' prefix
Using verbs without 'use'
Confusing composable with normal functions
3. Given this composable code:
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?
medium
A. 1
B. 2
C. 0
D. undefined
Solution
Step 1: Understand the initial state
The count ref starts at 0.
Step 2: Apply the increment function twice
Each call to increment() increases count.value by 1, so after two calls, count.value is 2.
Final Answer:
2 -> Option B
Quick Check:
Increment twice = count 2 [OK]
Hint: Increment twice means count.value becomes 2 [OK]
Common Mistakes:
Forgetting to access .value of ref
Assuming count resets
Confusing function call effects
4. Identify the error in this composable naming:
export function fetchData() {
// composable logic
}
medium
A. Composable functions cannot be exported
B. Function should be a class, not a function
C. Function name is too long
D. Missing 'use' prefix in function name
Solution
Step 1: Check naming conventions for composables
Composable functions must start with use to be recognized as such.
Step 2: Identify the missing prefix
The function fetchData lacks the use prefix, so it breaks the convention.
Final Answer:
Missing 'use' prefix in function name -> Option D
Quick Check:
Composable names need 'use' prefix [OK]
Hint: Composable names must start with 'use' prefix [OK]
Common Mistakes:
Ignoring naming conventions
Thinking export is disallowed
Confusing function types
5. You have multiple composables related to authentication: useAuthLogin, useAuthLogout, and useAuthStatus. Why is using the useAuth prefix a good practice?
hard
A. It groups related composables, making code easier to understand and maintain
B. It makes the composables run faster by grouping them
C. It hides the composables from other parts of the app
D. It automatically imports all composables with the prefix
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 A
Quick Check:
Prefix groups related composables [OK]
Hint: Use prefixes to group related composables clearly [OK]
Common Mistakes:
Assuming prefix affects performance
Thinking prefix hides composables
Believing prefix auto-imports functions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.