Consider this Vue 3 composable using the useFetch pattern. What will the data ref contain after the fetch completes successfully?
import { ref } from 'vue'; export function useFetch(url) { const data = ref(null); const error = ref(null); const loading = ref(true); fetch(url) .then(res => res.json()) .then(json => { data.value = json; loading.value = false; }) .catch(err => { error.value = err.message; loading.value = false; }); return { data, error, loading }; }
Think about what res.json() returns and what is assigned to data.value.
The composable calls fetch and then parses the response as JSON. The parsed JSON is assigned to data.value. So after success, data contains the API data.
loading after fetch fails?In the same useFetch composable, what will the loading ref value be if the fetch call fails (e.g., network error)?
import { ref } from 'vue'; export function useFetch(url) { const data = ref(null); const error = ref(null); const loading = ref(true); fetch(url) .then(res => res.json()) .then(json => { data.value = json; loading.value = false; }) .catch(err => { error.value = err.message; loading.value = false; }); return { data, error, loading }; }
Check what happens to loading.value in the catch block.
When fetch fails, the catch block sets loading.value to false to indicate loading is done despite the error.
You want to add an AbortController to the useFetch composable to cancel the fetch request if needed. Which option correctly implements this?
import { ref } from 'vue'; export function useFetch(url) { const data = ref(null); const error = ref(null); const loading = ref(true); // Add abort controller here fetch(url, { signal: controller.signal }) .then(res => res.json()) .then(json => { data.value = json; loading.value = false; }) .catch(err => { if (err.name === 'AbortError') { error.value = 'Fetch aborted'; } else { error.value = err.message; } loading.value = false; }); return { data, error, loading, controller }; }
Remember how to correctly instantiate a class in JavaScript.
The AbortController must be instantiated with new AbortController(). Omitting new or parentheses causes errors.
data?Look at this composable code. Why does data remain null even after the fetch completes?
import { ref } from 'vue'; export function useFetch(url) { const data = ref(null); const error = ref(null); const loading = ref(true); async function fetchData() { try { const res = await fetch(url); const json = await res.json(); data = json; loading.value = false; } catch (err) { error.value = err.message; loading.value = false; } } fetchData(); return { data, error, loading }; }
Remember how to update a Vue ref's value.
Assigning directly to data replaces the ref variable instead of updating its reactive value. The correct way is data.value = json;.
What is the main advantage of using a composable like useFetch for API calls in Vue 3 applications?
Think about code reuse and reactive state in Vue 3.
Composables encapsulate logic and reactive state, making it easy to reuse API fetching logic in many components without duplication.