0
0
Vueframework~20 mins

GET requests in components in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue GET Requests Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Vue component display after mounting?

Consider this Vue 3 component using the Composition API. It fetches user data from an API on mount.

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const user = ref(null);
    onMounted(async () => {
      const res = await fetch('https://jsonplaceholder.typicode.com/users/1');
      user.value = await res.json();
    });
    return { user };
  }
}

What will be rendered inside the template if it shows {{ user?.name || 'Loading...' }}?

AThrows a runtime error because user is null initially
BShows 'Loading...' forever because fetch is not awaited
CDisplays the user's name after data loads, initially shows 'Loading...'
DDisplays an empty string because user.name is undefined
Attempts:
2 left
💡 Hint

Think about how ref and onMounted work together with async fetch.

📝 Syntax
intermediate
2:00remaining
Which option correctly fetches data in a Vue 3 component's setup()?

Choose the code snippet that correctly fetches JSON data and assigns it to a ref inside setup() using async/await.

A
const data = ref(null);
onMounted(async () => {
  const res = await fetch('url');
  data.value = await res.json();
});
B
const data = ref(null);
await fetch('url').then(res => res.json()).then(json => data.value = json);
C
const data = ref(null);
const res = fetch('url');
data.value = res.json();
D
const data = ref(null);
fetch('url').then(res => data = res.json());
Attempts:
2 left
💡 Hint

Remember to use onMounted for side effects and update ref.value.

🔧 Debug
advanced
2:00remaining
Why does this Vue component fail to update after fetch?

Look at this Vue 3 component snippet:

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const info = ref(null);
    onMounted(() => {
      fetch('https://api.example.com/data')
        .then(response => response.json())
        .then(data => {
          info = data;
        });
    });
    return { info };
  }
}

Why does the template not update with the fetched data?

ABecause the API URL is invalid and fetch fails silently
BBecause fetch is not awaited inside onMounted
CBecause onMounted should not be used for fetch calls
DBecause info is reassigned directly instead of updating info.value
Attempts:
2 left
💡 Hint

Remember how to update reactive refs in Vue.

state_output
advanced
2:00remaining
What is the value of 'count' after this Vue component fetches data?

Given this Vue 3 component:

import { ref, onMounted } from 'vue';

export default {
  setup() {
    const count = ref(0);
    onMounted(async () => {
      const res = await fetch('https://jsonplaceholder.typicode.com/posts');
      const posts = await res.json();
      count.value = posts.length;
    });
    return { count };
  }
}

What will count.value be after the fetch completes?

A100
BThrows an error because posts is not an array
CUndefined
D0
Attempts:
2 left
💡 Hint

Check the API response shape at https://jsonplaceholder.typicode.com/posts.

🧠 Conceptual
expert
2:00remaining
Why use onMounted for GET requests in Vue components?

In Vue 3 Composition API, why is it best practice to place GET requests inside onMounted() instead of directly in setup()?

ABecause setup() cannot be async, so fetch must be in onMounted()
BBecause setup() runs before the component is mounted, so DOM or reactive context may not be ready for side effects
CBecause onMounted() delays the fetch until user interaction
DBecause fetch is not allowed inside setup() by Vue's rules
Attempts:
2 left
💡 Hint

Think about the component lifecycle and when side effects should run.