0
0
Vueframework~20 mins

Fetch API in Vue components - Practice Problems & Coding Challenges

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

Consider this Vue 3 component using the Fetch API inside onMounted. What will be shown in the template after the component loads?

Vue
<script setup>
import { ref, onMounted } from 'vue'
const data = ref(null)
onMounted(async () => {
  const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
  data.value = await response.json()
})
</script>
<template>
  <div>
    <p v-if="data">Title: {{ data.title }}</p>
    <p v-else>Loading...</p>
  </div>
</template>
AIt shows 'Title: delectus aut autem' after fetching the data successfully.
BIt shows 'Loading...' indefinitely because fetch is not awaited properly.
CIt throws a runtime error because 'data' is not reactive.
DIt shows an empty paragraph because 'data.title' is undefined.
Attempts:
2 left
💡 Hint

Check how ref and await are used inside onMounted.

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

Choose the correct code snippet that fetches JSON data inside the setup() function using async syntax.

A
async setup() {
  const data = ref(null)
  const response = await fetch('/api/data')
  data.value = await response.json()
  return { data }
}
B
setup() {
  const data = ref(null)
  fetch('/api/data').then(res =&gt; data.value = res.json())
  return { data }
}
C
setup() {
  const data = null
  const response = await fetch('/api/data')
  data = await response.json()
  return { data }
}
D
async setup() {
  const data = ref(null)
  fetch('/api/data').then(async res =&gt; data = await res.json())
  return { data }
}
Attempts:
2 left
💡 Hint

Remember that await can only be used inside async functions.

🔧 Debug
advanced
2:00remaining
Why does this Vue component fail to update the template after fetching data?

Examine this Vue 3 component code. It fetches data but the template never updates to show the fetched title. What is the cause?

Vue
<script setup>
import { ref, onMounted } from 'vue'
const data = null
onMounted(async () => {
  const response = await fetch('https://jsonplaceholder.typicode.com/todos/1')
  data = await response.json()
})
</script>
<template>
  <div>
    <p v-if="data">Title: {{ data.title }}</p>
    <p v-else>Loading...</p>
  </div>
</template>
AThe fetch URL is incorrect, so no data is returned.
BThe variable 'data' is not reactive because it is initialized as null, not with ref().
CThe 'onMounted' hook is not called in <script setup> syntax.
DThe template uses 'v-if' incorrectly and should use 'v-show' instead.
Attempts:
2 left
💡 Hint

Check how Vue tracks changes to variables for reactivity.

state_output
advanced
2:00remaining
What is the value of errorMessage after fetch failure in this Vue component?

This Vue 3 component tries to fetch data but the URL is invalid. What will be the value of errorMessage after the fetch attempt?

Vue
<script setup>
import { ref, onMounted } from 'vue'
const data = ref(null)
const errorMessage = ref('')
onMounted(async () => {
  try {
    const response = await fetch('https://invalid.url')
    if (!response.ok) throw new Error('Network response was not ok')
    data.value = await response.json()
  } catch (error) {
    errorMessage.value = error.message
  }
})
</script>
<template>
  <div>
    <p v-if="errorMessage">Error: {{ errorMessage }}</p>
    <p v-else-if="data">Data loaded</p>
    <p v-else>Loading...</p>
  </div>
</template>
A"Network response was not ok"
B"TypeError: Failed to fetch"
C"Failed to fetch"
D"" (empty string)
Attempts:
2 left
💡 Hint

Consider what error message the browser throws on network failure.

🧠 Conceptual
expert
2:00remaining
Why should you avoid using fetch directly inside the template in Vue?

Which reason best explains why calling fetch directly inside a Vue template expression is a bad idea?

ABecause fetch requires special Vue directives to work inside templates.
BBecause fetch is not supported in Vue templates due to security restrictions.
CBecause fetch modifies the DOM directly, which conflicts with Vue's virtual DOM.
DBecause fetch returns a Promise and templates cannot handle asynchronous operations, causing rendering issues.
Attempts:
2 left
💡 Hint

Think about how Vue templates render and handle data.