0
0
Vueframework~20 mins

Generic components in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generic Components Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this generic Vue component?

Consider this Vue 3 generic component using the Composition API:

<script setup lang="ts">
import { defineProps } from 'vue'

type Item = { id: number; name: string }

const props = defineProps<{ items: Item[] }>()
</script>

<template>
  <ul>
    <li v-for="item in props.items" :key="item.id">{{ item.name }}</li>
  </ul>
</template>

If you pass items=[{id:1, name:'Apple'}, {id:2, name:'Banana'}], what will be the output?

Vue
<script setup lang="ts">
import { defineProps } from 'vue'

type Item = { id: number; name: string }

const props = defineProps<{ items: Item[] }>()
</script>

<template>
  <ul>
    <li v-for="item in props.items" :key="item.id">{{ item.name }}</li>
  </ul>
</template>
A<ul><li>props.items</li></ul>
B<ul><li>1</li><li>2</li></ul>
C<ul><li>Apple</li><li>Banana</li></ul>
D<ul><li>{id:1, name:'Apple'}</li><li>{id:2, name:'Banana'}</li></ul>
Attempts:
2 left
💡 Hint

Look at what is displayed inside the <li> tags.

📝 Syntax
intermediate
2:00remaining
Which option correctly defines a generic Vue component prop type?

In Vue 3 with TypeScript, you want to define a generic component that accepts a prop data of type T[]. Which syntax is correct?

Aconst props = defineProps<{ data: T[] }>()
Bconst props = defineProps<{ data: Array<T> }>()
Cconst props = defineProps<T>()
Dconst props = defineProps<{ data: T }>()
Attempts:
2 left
💡 Hint

Remember that Array<T> and T[] are equivalent in TypeScript.

🔧 Debug
advanced
2:00remaining
Why does this generic Vue component fail to compile?

Given this Vue 3 component code snippet:

<script setup lang="ts">
import { defineProps } from 'vue'

type Item<T> = { id: number; value: T }

const props = defineProps<{ items: Item[] }>()
</script>

Why does it fail?

Vue
<script setup lang="ts">
import { defineProps } from 'vue'

type Item<T> = { id: number; value: T }

const props = defineProps<{ items: Item[] }>()
</script>
ABecause Item is a generic type but used without specifying T
BBecause defineProps cannot accept generic types
CBecause id must be a string, not number
DBecause value property is missing in props
Attempts:
2 left
💡 Hint

Check how generic types are used in TypeScript.

state_output
advanced
2:00remaining
What is the value of count after clicking the button twice in this generic Vue component?

Consider this Vue 3 generic component:

<script setup lang="ts">
import { ref } from 'vue'

const count = ref(0)

function increment() {
  count.value++
}
</script>

<template>
  <button @click="increment">Increment</button>
  <p>Count: {{ count.value }}</p>
</template>

What will be displayed after clicking the button twice?

Vue
<script setup lang="ts">
import { ref } from 'vue'

const count = ref(0)

function increment() {
  count.value++
}
</script>

<template>
  <button @click="increment">Increment</button>
  <p>Count: {{ count.value }}</p>
</template>
ACount: 2
BCount: 1
CCount: 0
DCount: undefined
Attempts:
2 left
💡 Hint

Each click increases count by 1.

🧠 Conceptual
expert
2:00remaining
Which statement about Vue 3 generic components is true?

Choose the correct statement about using generic components in Vue 3 with TypeScript.

AVue automatically infers generic types from prop values without any TypeScript annotations
BYou must explicitly specify generic type parameters when using generic components in templates
CGeneric components can only be defined using the Options API, not Composition API
DGeneric types in Vue props help ensure type safety but do not affect runtime behavior
Attempts:
2 left
💡 Hint

Think about TypeScript's role in Vue components.