A composable is a reusable function that uses Vue's Composition API to share reactive logic between components.
Click to reveal answer
beginner
Why do composables improve code organization?
Composables let you group related logic in one place, making code easier to read, maintain, and reuse across components.
Click to reveal answer
beginner
How do composables help avoid code duplication?
By extracting shared logic into composables, you write that logic once and use it in many components, avoiding repeated code.
Click to reveal answer
beginner
What is a real-life analogy for composables?
Composables are like kitchen tools you use for many recipes. Instead of making a new tool each time, you reuse the same tool for different dishes.
Click to reveal answer
intermediate
How do composables support testing in Vue?
Since composables are plain functions with logic, you can test them independently from components, making testing simpler and clearer.
Click to reveal answer
What is the main purpose of a composable in Vue?
ATo create new HTML elements
BTo style components with CSS
CTo share reactive logic between components
DTo manage routing between pages
✗ Incorrect
Composables are designed to share reactive logic, not styling, HTML creation, or routing.
How do composables help with code duplication?
ABy copying code into each component
BBy using inline styles
CBy removing all logic from components
DBy extracting shared logic into reusable functions
✗ Incorrect
Composables extract shared logic so it can be reused, avoiding duplication.
Which Vue API do composables primarily use?
AComposition API
BVuex API
CVue Router API
DOptions API
✗ Incorrect
Composables use the Composition API to create reusable logic.
Why are composables easier to test?
ABecause they are plain functions separate from components
BBecause they include UI elements
CBecause they use inline CSS
DBecause they handle routing
✗ Incorrect
Composables are plain functions, so you can test their logic without UI or routing concerns.
What is a good analogy for composables?
AA recipe book with many recipes
BA kitchen tool used for many dishes
CA single-use disposable plate
DA locked drawer with secret ingredients
✗ Incorrect
Composables are like reusable kitchen tools used in many recipes.
Explain why composables matter in Vue and how they improve your code.
Think about how sharing tools or recipes helps in cooking.
You got /4 concepts.
Describe a real-life example that helps you understand composables.
Think about tools or objects you use many times for different tasks.
You got /3 concepts.
Practice
(1/5)
1. Why are composables important in Vue applications?
easy
A. They compile Vue templates into JavaScript.
B. They automatically style components without CSS.
C. They replace the need for Vue Router.
D. They help reuse logic across multiple components easily.
Solution
Step 1: Understand composables' purpose
Composables are functions that let you reuse logic in Vue components.
Step 2: Identify the correct benefit
They help keep components clean by sharing logic, not styling or routing.
Final Answer:
They help reuse logic across multiple components easily. -> Option D
Quick Check:
Reusing logic = composables importance [OK]
Hint: Composables = reusable logic functions in Vue [OK]
Common Mistakes:
Thinking composables handle styling
Confusing composables with routing
Believing composables compile templates
2. Which of the following is the correct way to define a composable in Vue 3?
easy
A. function useCounter() { const count = ref(0); return { count }; }
B. const useCounter = new VueComponent() { count: 0 }
C. class useCounter { count = 0; }
D. const useCounter = () => { this.count = 0; }
Solution
Step 1: Recall composable syntax
Composables are functions that return reactive state or methods.
Step 2: Check each option
function useCounter() { const count = ref(0); return { count }; } defines a function returning a reactive ref, which is correct. Others misuse class, VueComponent, or this context.
Final Answer:
function useCounter() { const count = ref(0); return { count }; } -> Option A
Quick Check:
Composable = function returning reactive state [OK]
Hint: Composable must be a function returning reactive data [OK]
Common Mistakes:
Using class syntax for composables
Trying to use 'this' inside arrow functions
Confusing composables with components
3. Given this composable:
function useToggle() {
const on = ref(false);
function toggle() { on.value = !on.value; }
return { on, toggle };
}
What will be the value of on.value after calling toggle() twice starting from initial state?
medium
A. undefined
B. true
C. false
D. null
Solution
Step 1: Understand initial state
The ref on starts as false.
Step 2: Apply toggle twice
First toggle flips false to true. Second toggle flips true back to false.
Final Answer:
false -> Option C
Quick Check:
Toggle twice returns to initial false [OK]
Hint: Toggle flips boolean; two flips return to start [OK]
Common Mistakes:
Assuming toggle only flips once
Confusing ref.value with ref itself
Expecting undefined or null values
4. What is wrong with this composable code?
function useData() {
const data = ref(null);
fetch('api/data').then(response => {
data.value = response.json();
});
return { data };
}
medium
A. fetch returns a promise, so data.value is set to a promise, not actual data.
B. ref cannot hold null values initially.
C. The composable should not return data.
D. fetch cannot be used inside composables.
Solution
Step 1: Analyze fetch usage
fetch returns a promise; response.json() also returns a promise.
Step 2: Identify mistake in assigning data.value
Assigning data.value = response.json() sets it to a promise, not resolved data.
Final Answer:
fetch returns a promise, so data.value is set to a promise, not actual data. -> Option A
Quick Check:
response.json() returns promise, must await [OK]
Hint: Remember to await response.json() before assigning [OK]
Common Mistakes:
Not awaiting response.json()
Thinking ref can't start as null
Believing fetch is disallowed in composables
5. You want to share a composable that tracks window width and updates reactively on resize. Which approach correctly uses Vue composables to achieve this?