0
0
Vueframework~20 mins

Composable for API calls (useFetch pattern) in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Fetch Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue composable return after a successful fetch?

Consider this Vue 3 composable using the useFetch pattern. What will the data ref contain after the fetch completes successfully?

Vue
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 };
}
AA ref containing the parsed JSON response from the API
BA ref containing the raw Response object from fetch
CA ref containing an error message string
DA ref containing the fetch Promise object
Attempts:
2 left
💡 Hint

Think about what res.json() returns and what is assigned to data.value.

state_output
intermediate
2:00remaining
What is the value of 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)?

Vue
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 };
}
Afalse
Btrue
Cnull
Dundefined
Attempts:
2 left
💡 Hint

Check what happens to loading.value in the catch block.

📝 Syntax
advanced
2:00remaining
Which option correctly adds an abort controller to cancel fetch?

You want to add an AbortController to the useFetch composable to cancel the fetch request if needed. Which option correctly implements this?

Vue
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 };
}
Aconst controller = AbortController();
Bconst controller = new AbortController();
Cconst controller = new AbortController;
Dconst controller = AbortController;
Attempts:
2 left
💡 Hint

Remember how to correctly instantiate a class in JavaScript.

🔧 Debug
advanced
2:00remaining
Why does this composable never update data?

Look at this composable code. Why does data remain null even after the fetch completes?

Vue
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 };
}
ABecause error is not handled
BBecause fetch is not awaited properly
CBecause loading is never set to false
DBecause 'data = json;' overwrites the ref instead of setting 'data.value'
Attempts:
2 left
💡 Hint

Remember how to update a Vue ref's value.

🧠 Conceptual
expert
2:00remaining
Why use a composable for API calls in Vue 3?

What is the main advantage of using a composable like useFetch for API calls in Vue 3 applications?

AIt replaces the need for Vuex or Pinia state management
BIt automatically caches API responses without extra code
CIt allows reusing reactive fetching logic across multiple components easily
DIt forces all API calls to be synchronous
Attempts:
2 left
💡 Hint

Think about code reuse and reactive state in Vue 3.