0
0
Vueframework~10 mins

Composables for reusable logic in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Composables for reusable logic
Create composable function
Define reactive state inside composable
Return state and functions
Import composable in component
Use returned state and functions
Component reacts to composable state changes
Reusable logic shared across components
This flow shows how you create a composable function with reactive state and logic, then use it inside components to share behavior easily.
Execution Sample
Vue
import { ref } from 'vue'

export function useCounter() {
  const count = ref(0)
  function increment() { count.value++ }
  return { count, increment }
}
Defines a composable that provides a reactive count and an increment function.
Execution Table
StepActionState BeforeState AfterOutput/Effect
1Call useCounter()No countcount = 0Composable returns count and increment
2Component uses count.valuecount = 0count = 0Component shows count as 0
3Call increment()count = 0count = 1count.value increments to 1
4Component reacts to count changecount = 1count = 1UI updates to show count as 1
5Call increment() againcount = 1count = 2count.value increments to 2
6Component reacts to count changecount = 2count = 2UI updates to show count as 2
7No more actionscount = 2count = 2Execution stops
💡 No more actions to perform, composable state stable
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 5Final
count.valueundefined0122
Key Moments - 2 Insights
Why does count.value update inside the component when increment() is called?
Because count is a reactive ref returned by the composable, when increment() changes count.value, Vue tracks this and updates the component automatically (see execution_table steps 3 and 4).
Can multiple components use the same composable and keep their own count?
Yes, each call to useCounter() creates a new reactive count ref, so each component has its own independent state (not shown in table but implied by composable pattern).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of count after increment() is called?
A0
Bundefined
C1
D2
💡 Hint
Check the 'State After' column at step 3 in execution_table
At which step does the component update its UI to show the new count value?
AStep 2
BStep 4
CStep 5
DStep 1
💡 Hint
Look for 'UI updates' in the Output/Effect column in execution_table
If we call increment() twice, what will count.value be after the second call?
A2
B0
C1
Dundefined
💡 Hint
See variable_tracker for count.value changes after steps 3 and 5
Concept Snapshot
Composable functions in Vue are reusable logic blocks.
They use reactive state like ref() inside.
Return state and functions to components.
Each component calling composable gets independent state.
Components react automatically to reactive changes.
Use composables to share logic cleanly.
Full Transcript
This visual trace shows how Vue composables work. First, you create a composable function that defines reactive state using ref(). It returns this state and any functions that change it. When a component calls this composable, it gets its own reactive state. Calling functions like increment() updates the reactive state. Vue detects these changes and updates the component's UI automatically. Each component using the composable has its own independent state. This pattern helps share logic easily and keeps components clean.