A composable is a reusable function that uses Vue's Composition API features like ref and reactive to share logic between components.
Click to reveal answer
beginner
How do you pass parameters to a composable in Vue?
You pass parameters by defining them as function arguments when creating the composable. This allows the composable to use dynamic values.
Click to reveal answer
intermediate
Why use parameters in composables?
Parameters make composables flexible and reusable by allowing different inputs to customize their behavior for each use case.
Click to reveal answer
beginner
Example: What does this composable do?
<pre>import { ref } from 'vue';
function useCounter(start = 0) {
const count = ref(start);
const increment = () => count.value++;
return { count, increment };
}</pre>
This composable creates a counter starting at the given start value (default 0). It returns a reactive count and an increment function to increase it.
Click to reveal answer
beginner
How does Vue's ref help in composables?
ref creates a reactive value that Vue tracks for changes. When used in composables, it allows components to reactively update when the composable's state changes.
Click to reveal answer
How do you make a composable accept a starting value in Vue?
ABy hardcoding the value inside the composable
BBy using a global variable
CBy adding a parameter to the composable function
DBy passing props to the composable
✗ Incorrect
Composable functions accept parameters like any normal function, allowing dynamic input.
What Vue feature makes the composable's state reactive?
Aref
Bprops
Cemit
Dwatch
✗ Incorrect
ref creates reactive data that Vue tracks for changes.
Which of these is a benefit of passing parameters to composables?
APrevents composables from being reactive
BMakes composables reusable with different data
CForces composables to be global
DDisables component updates
✗ Incorrect
Parameters allow composables to work with different inputs, increasing reuse.
What is the default value of the parameter in this composable?
function useCounter(start = 0) { ... }
A1
Bundefined
Cnull
D0
✗ Incorrect
The parameter start has a default value of 0.
How do you access the reactive value inside a ref in Vue?
AUsing the <code>.value</code> property
BDirectly like a normal variable
CUsing <code>.reactive</code>
DUsing <code>.data</code>
✗ Incorrect
Reactive values inside ref are accessed with .value.
Explain how to create a composable in Vue that accepts parameters and why this is useful.
Think about how functions normally take inputs and how that applies to composables.
You got /5 concepts.
Describe how Vue's ref works inside a composable and how you use it with parameters.
Consider how reactive state and parameters combine to make composables powerful.
You got /5 concepts.
Practice
(1/5)
1.
What is the main purpose of passing parameters to a Vue composable function?
easy
A. To customize the composable's behavior for different uses
B. To make the composable function run only once
C. To prevent the composable from using Vue's reactivity
D. To automatically render a template inside the composable
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 A
Quick Check:
Parameters customize composables = A [OK]
Hint: Parameters let composables adapt to different inputs [OK]
Common Mistakes:
Thinking composables run only once
Believing parameters disable reactivity
Confusing composables with templates
2.
Which of the following is the correct way to define a composable that accepts a parameter initialCount?
The composable receives 'Alice' as the name parameter, so the greeting string becomes "Hello, Alice!".
Step 2: Accessing the ref value
The greeting is a ref, so to get the string, we use greeting.value, which logs "Hello, Alice!".
Final Answer:
"Hello, Alice!" -> Option B
Quick Check:
Ref .value logs "Hello, Alice!" = C [OK]
Hint: Use .value to get ref content in Vue composables [OK]
Common Mistakes:
Logging the ref object instead of its value
Confusing parameter name with string literal
Forgetting to import ref from 'vue'
4.
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 }
}
medium
A. Cannot increment a ref directly with ++ operator
B. Missing return statement for increment function
C. Parameter start is not reactive
D. Ref should be initialized with 0, not start
Solution
Step 1: Check how ref values are updated
Refs hold their value inside the .value property, so you cannot increment the ref itself.
Step 2: Identify correct increment syntax
The increment function should update time.value++ instead of time++.
Final Answer:
Cannot increment a ref directly with ++ operator -> Option A
Quick Check:
Update ref via .value, not ref itself = B [OK]
Hint: Use .value to update ref, not the ref variable itself [OK]
Common Mistakes:
Trying to increment ref without .value
Thinking parameters must be reactive
Forgetting to return functions from composable
5.
You want a composable useFilteredList that accepts an array and a filter function as parameters. Which implementation correctly applies the filter reactively?