A composable is a reusable function in Vue that can accept parameters to customize its behavior. This helps you write clean and flexible code.
Composable accepting parameters in Vue
import { ref } from 'vue'; export function useExample(param) { const state = ref(param); function updateState(newVal) { state.value = newVal; } return { state, updateState }; }
The parameter param can be any value you want to customize the composable.
Use ref or reactive to create reactive state inside the composable.
import { ref } from 'vue'; export function useCounter(start = 0) { const count = ref(start); function increment() { count.value++; } return { count, increment }; }
import { ref } from 'vue'; export function useGreeting(name) { const greeting = ref(`Hello, ${name}!`); function updateName(newName) { greeting.value = `Hello, ${newName}!`; } return { greeting, updateName }; }
This Vue component uses the useCounter composable with a starting value of 5. It shows the count and a button to increase it.
<script setup> import { useCounter } from './useCounter'; const { count, increment } = useCounter(5); </script> <template> <div> <p>Count: {{ count }}</p> <button @click="increment">Add 1</button> </div> </template>
Always pass parameters to composables to make them flexible and reusable.
Parameters can be simple values or objects for more complex configurations.
Use ref or reactive inside composables to keep state reactive.
Composable functions can accept parameters to customize their behavior.
This helps reuse logic with different inputs across components.
Use Vue's reactivity inside composables to keep state updated.
Practice
What is the main purpose of passing parameters to a Vue composable function?
Solution
Step 1: Understand composable functions
Composable functions are reusable pieces of logic in Vue that can be shared across components.Step 2: Role of parameters in composables
Parameters allow passing different inputs to the composable so it can behave differently depending on the need.Final Answer:
To customize the composable's behavior for different uses -> Option AQuick Check:
Parameters customize composables = A [OK]
- Thinking composables run only once
- Believing parameters disable reactivity
- Confusing composables with templates
Which of the following is the correct way to define a composable that accepts a parameter initialCount?
function useCounter(???) {
const count = ref(initialCount)
return { count }
}Solution
Step 1: Check JavaScript function parameter syntax
In JavaScript, function parameters are named without types or parentheses inside the parentheses.Step 2: Match parameter name with usage
The parameterinitialCountmust be declared inside the parentheses asinitialCount.Final Answer:
useCounter(initialCount) -> Option CQuick Check:
Function parameter syntax = D [OK]
- Adding types like in TypeScript without syntax
- Using parentheses inside parameter list
- Leaving parameters empty when needed
Given this composable and usage, what will be logged?
import { ref } from 'vue'
function useGreeting(name) {
const greeting = ref(`Hello, ${name}!`)
return { greeting }
}
const { greeting } = useGreeting('Alice')
console.log(greeting.value)Solution
Step 1: Understand parameter passing
The composable receives 'Alice' as thenameparameter, so the greeting string becomes "Hello, Alice!".Step 2: Accessing the ref value
Thegreetingis a ref, so to get the string, we usegreeting.value, which logs "Hello, Alice!".Final Answer:
"Hello, Alice!" -> Option BQuick Check:
Ref .value logs "Hello, Alice!" = C [OK]
- Logging the ref object instead of its value
- Confusing parameter name with string literal
- Forgetting to import ref from 'vue'
Identify the error in this composable that accepts a parameter start:
import { ref } from 'vue'
function useTimer(start) {
const time = ref(start)
function increment() {
time++
}
return { time, increment }
}Solution
Step 1: Check how ref values are updated
Refs hold their value inside the.valueproperty, so you cannot increment the ref itself.Step 2: Identify correct increment syntax
The increment function should updatetime.value++instead oftime++.Final Answer:
Cannot increment a ref directly with ++ operator -> Option AQuick Check:
Update ref via .value, not ref itself = B [OK]
- Trying to increment ref without .value
- Thinking parameters must be reactive
- Forgetting to return functions from composable
You want a composable useFilteredList that accepts an array and a filter function as parameters. Which implementation correctly applies the filter reactively?
import { computed } from 'vue'
function useFilteredList(list, filterFn) {
const filtered = computed(() => ??? )
return { filtered }
}What should replace ????
Solution
Step 1: Understand composable parameters
Thelistparameter is expected to be aref(array)for proper reactivity in Vue composables.Step 2: Reactive filtering with computed
Inside thecomputed, access the array value withlist.value.filter(filterFn)to apply the filter reactively whenever the list changes.Final Answer:
list.value.filter(filterFn) -> Option DQuick Check:
computed(() => list.value.filter(filterFn)) = C [OK]
- Using list.filter(filterFn) without .value when list is ref
- Calling filterFn(list) instead of list.filter(filterFn)
- Unnecessarily nesting computed inside computed
Ready to prove it?
Pass the test and this pattern moves unknown
