0
0
Vueframework~10 mins

Composable naming conventions (use prefix) in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Composable naming conventions (use prefix)
Create composable function
Add prefix 'use' to name
Export composable
Import composable in component
Call composable to get reactive data
Use reactive data in template
This flow shows how to create a composable function with a 'use' prefix, export it, import it in a Vue component, and use its reactive data.
Execution Sample
Vue
import { ref } from 'vue'

export function useCounter() {
  const count = ref(0)
  function increment() { count.value++ }
  return { count, increment }
}
Defines a composable named 'useCounter' that provides a reactive count and an increment function.
Execution Table
StepActionName UsedResultNotes
1Define composable functionuseCounterFunction createdPrefix 'use' signals composable
2Inside composable, create reactive statecountRef with initial 0Reactive state ready
3Define increment functionincrementFunction increments countUpdates reactive state
4Return reactive state and functioncount, incrementReturned as objectComposable API exposed
5Import composable in componentuseCounterComposable importedReady to use in component
6Call composable in setup()useCounter()Get count and incrementReactive data available
7Use count in templatecountDisplays current countReactive updates reflected
8Call increment on eventincrement()count.value increasesUI updates automatically
9ExitN/AComposable used successfullyNaming convention helps clarity
💡 Composable usage ends after reactive data is used and updated in component
Variable Tracker
VariableStartAfter 1After 2Final
count.value0002
incrementfunctionfunctionfunctionfunction
Key Moments - 3 Insights
Why do we prefix composable functions with 'use'?
The 'use' prefix clearly identifies the function as a composable, making it easy to recognize in the code. See execution_table step 1 where 'useCounter' is defined.
What happens if we don't use the 'use' prefix?
Without the prefix, it is harder to tell if a function is a composable or a regular function, which can confuse developers. The naming convention in step 1 helps avoid this.
How does the reactive state 'count' update in the component?
The 'count' is a ref returned by the composable. When 'increment' is called (step 8), 'count.value' increases and Vue automatically updates the UI. See variable_tracker for count changes.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table step 6. What does calling useCounter() return?
AAn object with reactive count and increment function
BA number representing the count
CA string with the name 'useCounter'
DNothing, it just runs code
💡 Hint
Check the 'Result' column in step 6 of execution_table
At which step does the reactive count value increase?
AStep 3
BStep 5
CStep 8
DStep 2
💡 Hint
Look for when increment() is called in execution_table
If we rename 'useCounter' to 'counter', what is the main impact?
ANo impact, code works the same
BIt becomes harder to identify it as a composable
CIt breaks the reactive state
DThe increment function stops working
💡 Hint
Refer to key_moments about the importance of the 'use' prefix
Concept Snapshot
Composable functions in Vue should start with 'use' prefix.
This naming helps identify them as reusable reactive logic.
They return reactive state and functions.
Import and call them inside setup() of components.
Use returned reactive data in templates for automatic updates.
Full Transcript
This visual execution shows how to create and use a Vue composable function with the 'use' prefix. First, we define a function named 'useCounter' that creates a reactive count variable and an increment function. We export this composable. Then, in a Vue component, we import and call 'useCounter' inside the setup function to get reactive data. The count value is used in the template and updates automatically when increment is called. The 'use' prefix helps developers quickly recognize composables in code, improving clarity and maintainability.