0
0
Vueframework~10 mins

Composable accepting parameters in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Composable accepting parameters
Call composable with params
Inside composable: receive params
Use params to create reactive state
Return reactive state and functions
Component uses returned state
Component renders with param-based data
The component calls the composable with parameters, which creates reactive state based on those parameters and returns it for the component to use.
Execution Sample
Vue
import { ref } from 'vue'

function useCounter(start) {
  const count = ref(start)
  function increment() { count.value++ }
  return { count, increment }
}
This composable accepts a starting number and returns a reactive count and an increment function.
Execution Table
StepActionParameter 'start'count.valueincrement() callOutput
1Composable called with start=55undefinedNoComposable initializes count with 5
2count initialized as ref(start)55Nocount.value is 5
3increment() called56Yescount.value increments to 6
4increment() called again57Yescount.value increments to 7
5Component reads count.value57NoRendered count is 7
6No more actions57NoExecution ends
💡 No more increment calls; component uses final count value 7
Variable Tracker
VariableStartAfter 1After 2After 3Final
startundefined5555
count.valueundefined5677
Key Moments - 2 Insights
Why does count.value start at the parameter 'start' value?
Because the composable uses ref(start) to create reactive state initialized with the passed parameter, as shown in execution_table step 2.
What happens when increment() is called multiple times?
Each call increases count.value by 1, updating the reactive state, as seen in steps 3 and 4 of the execution_table.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is count.value after the first increment() call?
A6
B5
C7
DUndefined
💡 Hint
Check execution_table row 3 under count.value
At which step does the composable initialize count with the parameter value?
AStep 1
BStep 2
CStep 3
DStep 5
💡 Hint
Look at execution_table where count.value first gets the start value
If the composable was called with start=10 instead of 5, what would count.value be after two increments?
A10
B7
C12
D11
💡 Hint
Refer to variable_tracker and execution_table logic for increments
Concept Snapshot
Composable accepting parameters:
- Define composable as a function with parameters
- Use parameters to initialize reactive state (e.g., ref(start))
- Return reactive state and functions
- Component calls composable with arguments
- State updates reflect parameter-based initialization
Full Transcript
A composable in Vue is a function that can accept parameters. When you call it, you pass in values that it uses to create reactive state. For example, a composable called useCounter can take a starting number. Inside, it creates a reactive count initialized to that number. It also provides a function to increment the count. When the component uses this composable, it gets the reactive count and the increment function. Calling increment updates the count, and the component re-renders with the new value. This flow shows how parameters customize composables and how state changes over time.