0
0
Vueframework~20 mins

Loading states pattern in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Loading States Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will the component display during data loading?

Consider this Vue 3 component using the Composition API. What will the user see while the data is being fetched?

Vue
<script setup>
import { ref, onMounted } from 'vue'

const loading = ref(true)
const data = ref(null)

onMounted(() => {
  setTimeout(() => {
    data.value = 'Hello Vue!'
    loading.value = false
  }, 2000)
})
</script>

<template>
  <div>
    <p v-if="loading">Loading data...</p>
    <p v-else>{{ data }}</p>
  </div>
</template>
AAn error message is displayed because 'data' is null initially.
BThe component shows 'Hello Vue!' immediately without delay.
CThe text 'Loading data...' appears for 2 seconds, then 'Hello Vue!' is shown.
DThe component remains blank until data is loaded.
Attempts:
2 left
💡 Hint

Look at the loading variable and how it controls the displayed text.

state_output
intermediate
1:30remaining
What is the value of 'isLoading' after fetchData() resolves?

In this Vue 3 component, what will be the value of isLoading after fetchData() finishes?

Vue
<script setup>
import { ref } from 'vue'

const isLoading = ref(false)
const data = ref(null)

async function fetchData() {
  isLoading.value = true
  data.value = await new Promise(resolve => setTimeout(() => resolve('Done'), 1000))
  isLoading.value = false
}

fetchData()
</script>
Afalse
Btrue
Cnull
Dundefined
Attempts:
2 left
💡 Hint

Check when isLoading is set to false in the async function.

📝 Syntax
advanced
2:30remaining
Which option correctly implements a loading state with Vue 3's Composition API?

Choose the code snippet that correctly manages a loading state while fetching data asynchronously.

A
&lt;script setup&gt;
import { ref } from 'vue'
const loading = ref(false)
async function load() {
  loading.value = true
  fetch('/api/data')
  loading.value = false
}
load()
&lt;/script&gt;
B
&lt;script setup&gt;
import { reactive } from 'vue'
const state = reactive({ loading: false })
function load() {
  state.loading = true
  fetch('/api/data').then(() =&gt; {
    state.loading = false
  })
}
load()
&lt;/script&gt;
C
&lt;script setup&gt;
import { ref } from 'vue'
const loading = false
async function load() {
  loading = true
  await fetch('/api/data')
  loading = false
}
load()
&lt;/script&gt;
D
&lt;script setup&gt;
import { ref } from 'vue'
const loading = ref(false)
function load() {
  loading = true
  fetch('/api/data').then(() =&gt; {
    loading = false
  })
}
load()
&lt;/script&gt;
Attempts:
2 left
💡 Hint

Remember how to correctly update reactive state in Vue 3.

🔧 Debug
advanced
2:30remaining
Why does the loading spinner disappear too soon in this Vue component?

Examine the code below. The loading spinner shows briefly but disappears too soon. What is the cause?

Vue
<script setup>
import { ref, onMounted } from 'vue'

const loading = ref(true)

onMounted(() => {
  fetch('/api/data')
    .then(response => response.json())
    .then(data => {
      // process data
    })
  loading.value = false
})
</script>

<template>
  <div>
    <div v-if="loading">Loading spinner...</div>
    <div v-else>Data loaded</div>
  </div>
</template>
Aloading.value is set to false immediately after fetch starts, so spinner disappears too soon.
Bloading.value is never set to false because the fetch promise is not awaited before setting loading.
CThe fetch call is missing error handling, causing loading to stay true on failure.
DThe loading ref is not reactive, so changes do not update the template.
Attempts:
2 left
💡 Hint

Check when loading.value is updated relative to the fetch call.

🧠 Conceptual
expert
3:00remaining
Which approach best ensures accessibility during loading states in Vue components?

When showing a loading spinner in a Vue component, which practice best improves accessibility for screen reader users?

AUse <code>aria-hidden="true"</code> on the spinner to hide it from screen readers.
BOnly show a spinner icon with no text or ARIA attributes to keep UI clean.
CDisable all keyboard interactions until loading finishes without announcing loading state.
DUse <code>aria-busy="true"</code> on the container and provide visually hidden text describing loading status.
Attempts:
2 left
💡 Hint

Think about how screen readers detect loading and how to inform users.