A composable is a reusable function that uses Vue's Composition API features like refs and reactive state to share logic across components.
Click to reveal answer
beginner
What is the main purpose of the useFetch composable pattern?
The useFetch pattern helps to fetch data from APIs in a reusable way, managing loading, error, and data states inside a composable function.
Click to reveal answer
intermediate
Which Vue 3 feature allows useFetch to track loading and error states reactively?
Vue 3's ref and reactive functions allow useFetch to create reactive variables for loading, error, and data states.
Click to reveal answer
intermediate
How does useFetch improve code reuse compared to fetching data directly inside components?
useFetch centralizes API call logic, so multiple components can share the same fetching code without repeating it, making code cleaner and easier to maintain.
Click to reveal answer
intermediate
What is a common way to handle errors inside a useFetch composable?
Inside useFetch, errors are caught in a try-catch block and stored in a reactive error variable, so components can reactively show error messages.
Click to reveal answer
What does the useFetch composable typically return?
AA promise that resolves with the data
BReactive references for data, loading, and error states
COnly the fetched data as a plain object
DA Vue component
✗ Incorrect
useFetch returns reactive refs for data, loading, and error so components can reactively respond to API call states.
Which Vue Composition API function is commonly used inside useFetch to hold the loading state?
Aref
Bcomputed
Cwatch
Dprovide
✗ Incorrect
ref creates a reactive variable, perfect for tracking loading state inside useFetch.
Why should API calls be placed inside a composable like useFetch instead of directly in components?
ATo avoid using Vue's reactive system
BBecause components cannot make API calls
CTo reuse fetching logic and keep components clean
DTo make the app slower
✗ Incorrect
Placing API calls in composables promotes code reuse and cleaner components.
How does useFetch handle asynchronous API calls?
AUsing async/await inside the composable function
BUsing setTimeout to delay calls
CUsing synchronous XMLHttpRequest
DUsing Vue directives
✗ Incorrect
useFetch uses async/await to handle asynchronous API calls cleanly.
What should you do inside useFetch if the API call fails?
AIgnore the error silently
BThrow the error to the console only
CReload the page automatically
DCatch the error and update a reactive error variable
✗ Incorrect
Catching errors and updating a reactive error variable lets components show error messages reactively.
Explain how you would create a useFetch composable in Vue 3 to fetch data from an API and handle loading and error states.
Think about how to track states reactively and handle async calls inside a function.
You got /4 concepts.
Describe the benefits of using a composable like useFetch for API calls in a Vue 3 application.
Consider how sharing logic helps when multiple components need the same data.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of a composable like useFetch in Vue?
easy
A. To fetch data from an API and manage loading and error states reactively
B. To style components with CSS variables
C. To create Vue directives for DOM manipulation
D. To define routes in a Vue application
Solution
Step 1: Understand composable role
A composable in Vue is a reusable function that encapsulates logic, often for state or side effects.
Step 2: Identify useFetch purpose
The useFetch composable specifically handles API calls and manages reactive states like loading and error.
Final Answer:
To fetch data from an API and manage loading and error states reactively -> Option A
Quick Check:
Composable useFetch = fetch + reactive states [OK]
Hint: Composable useFetch manages API data and states reactively [OK]
Common Mistakes:
Confusing composables with styling or routing
Thinking composables only handle UI rendering
Assuming composables are Vue components
2. Which of the following is the correct way to import and use a useFetch composable in a Vue component?
easy
A. const { useFetch } = Vue; const result = useFetch(url);
B. import useFetch from 'vue'; const data = useFetch(url);
C. import { useFetch } from '@/composables/useFetch'; const { data, error } = useFetch(url);
D. import { fetchData } from '@/composables'; fetchData(url);
Solution
Step 1: Check import syntax
The composable is usually exported from a file and imported with curly braces if it's a named export.
Step 2: Using the composable
Calling useFetch(url) returns reactive properties like data and error.
Final Answer:
import { useFetch } from '@/composables/useFetch'; const { data, error } = useFetch(url); -> Option C
A. Setting loading.value = true after fetch call starts causes race condition
B. Not using async/await causes syntax error
C. data should be initialized as an empty array, not null
D. Error ref should be initialized as an empty string
Solution
Step 1: Analyze loading state timing
Loading is set to false inside fetch callbacks but set to true after fetch starts, which can cause timing issues.
Step 2: Identify race condition
Because loading is set to true after fetch call, it might overwrite loading false set in callbacks if fetch resolves quickly.
Final Answer:
Setting loading.value = true after fetch call starts causes race condition -> Option A
Quick Check:
Loading state timing error = Setting loading.value = true after fetch call starts causes race condition [OK]
Hint: Set loading true before fetch call to avoid race [OK]
Common Mistakes:
Setting loading true after fetch call instead of before
Confusing async/await with promises syntax errors
Assuming initial data type must be array
5. You want to create a useFetch composable that automatically retries fetching up to 3 times if the API call fails. Which approach correctly adds this retry logic?
hard
A. Wrap fetch in a try/catch but do not retry on failure
B. Call fetch once and ignore errors, relying on component to retry
C. Use setTimeout outside the composable to call useFetch repeatedly
D. Use a recursive function inside the composable that calls fetch again on error until max retries reached