A composable is a function that uses Vue's Composition API to encapsulate and reuse reactive logic across components.
Click to reveal answer
beginner
How do composables help in Vue development?
Composables help by organizing code into reusable pieces, making components cleaner and logic easier to share and test.
Click to reveal answer
intermediate
Which Vue Composition API functions are commonly used inside composables?
Functions like ref, reactive, computed, and watch are commonly used to create reactive state and side effects inside composables.
Click to reveal answer
beginner
How do you use a composable inside a Vue component?
You import the composable function and call it inside the <code>setup()</code> function of your component to access its reactive state and methods.
Click to reveal answer
intermediate
Why should composables avoid using component-specific code like template refs or DOM manipulation?
Because composables are meant to be reusable logic units, they should stay independent of component-specific details to keep them flexible and testable.
Click to reveal answer
What is the main purpose of a composable in Vue?
ATo reuse reactive logic across components
BTo style components with CSS
CTo define HTML templates
DTo manage Vuex state
✗ Incorrect
Composables are functions that encapsulate reactive logic for reuse, not for styling or templates.
Where do you call a composable function inside a Vue component?
AInside the <code>template</code> section
BInside the <code>methods</code> option
CInside the <code>data()</code> option
DInside the <code>setup()</code> function
✗ Incorrect
Composable functions are called inside the setup() function to access reactive state.
Which of these is NOT typically part of a composable?
AReactive state with <code>ref</code>
BComputed properties
CDirect DOM manipulation
DWatchers for side effects
✗ Incorrect
Composables should avoid direct DOM manipulation to stay reusable and independent.
What Vue API is essential for creating reactive variables in composables?
A<code>template</code>
B<code>ref</code>
C<code>emit</code>
D<code>props</code>
✗ Incorrect
ref creates reactive variables used inside composables.
Why is it good to use composables in Vue projects?
AThey make code reusable and easier to maintain
BThey automatically generate CSS styles
CThey replace the need for components
DThey handle routing between pages
✗ Incorrect
Composables help organize and reuse logic, improving maintainability.
Explain what a composable is in Vue and how it helps with code reuse.
Think about how you can share logic between components without repeating code.
You got /5 concepts.
Describe best practices when writing composables to keep them reusable and clean.
Consider what makes a piece of code easy to reuse in different places.
You got /5 concepts.
Practice
(1/5)
1. What is the main purpose of a composable in Vue?
easy
A. To reuse reactive logic across multiple components
B. To define global CSS styles
C. To create new Vue directives
D. To register Vue components globally
Solution
Step 1: Understand composables role
Composables are functions that encapsulate reactive logic to be reused.
Step 2: Identify main purpose
They help share logic like state or effects across components without duplication.
Final Answer:
To reuse reactive logic across multiple components -> Option A
Quick Check:
Composable = reusable logic [OK]
Hint: Composables share logic, not styles or components [OK]
Common Mistakes:
Confusing composables with CSS or templates
Thinking composables register components
Assuming composables create directives
2. Which of the following is the correct way to define a composable in Vue 3?
easy
A. function useCounter() { return reactive({ count: 0 }) }
B. const useCounter = new Vue({ data() { return { count: 0 } } })
C. const useCounter = reactive({ count: 0 })
D. function useCounter() { const count = ref(0); return { count }; }
Solution
Step 1: Check composable function syntax
A composable is a function that returns reactive state or methods.
Step 2: Validate options
function useCounter() { const count = ref(0); return { count }; } defines a function returning a ref wrapped count, which is correct. const useCounter = new Vue({ data() { return { count: 0 } } }) wrongly uses Vue instance syntax. const useCounter = reactive({ count: 0 }) returns reactive object directly without function. function useCounter() { return reactive({ count: 0 }) } returns reactive but misses ref usage and best practice.
Final Answer:
function useCounter() { const count = ref(0); return { count }; } -> Option D
Quick Check:
Composable = function returning reactive state [OK]
Hint: Composable must be a function returning reactive data [OK]
Common Mistakes:
Defining composables as Vue instances
Returning reactive objects without a function
Not using ref or reactive inside composables
3. Given this composable:
import { ref } from 'vue';
export function useToggle() {
const isOn = ref(false);
function toggle() { isOn.value = !isOn.value; }
return { isOn, toggle };
}
What will be the value of isOn.value after calling toggle() twice starting from initial state?
medium
A. true
B. undefined
C. false
D. null
Solution
Step 1: Analyze initial state and toggle function
Initially, isOn.value is false. Each toggle flips it to the opposite.
Step 2: Apply two toggles
First toggle: false -> true. Second toggle: true -> false.
Final Answer:
false -> Option C
Quick Check:
Two toggles flip false twice back to false [OK]
Hint: Toggle flips boolean; even toggles return original [OK]
Common Mistakes:
Assuming toggle sets true always
Forgetting to access .value of ref
Confusing initial state with toggled state
4. What is wrong with this composable code?
import { ref } from 'vue';
export function useCount() {
const count = ref(0);
function increment() {
count++;
}
return { count, increment };
}
medium
A. The count variable should be declared with reactive()
B. You cannot increment a ref directly with ++ operator
C. ref should be imported from 'vue/reactive' instead
D. The function should be named useCounter, not useCount
Solution
Step 1: Understand ref usage
Ref values are accessed and mutated via the .value property.
Step 2: Identify error in increment function
The code uses count++ which tries to increment the ref object itself, not its value. It should be count.value++.
Final Answer:
You cannot increment a ref directly with ++ operator -> Option B
Quick Check:
Use count.value++ to update ref [OK]
Hint: Always use .value to read or write ref values [OK]
Common Mistakes:
Incrementing ref without .value
Confusing reactive and ref imports
Thinking function name affects behavior
5. You want to create a composable that tracks window width and updates reactively on resize. Which approach correctly implements this?