0
0
Vueframework~20 mins

Recursive components in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Recursive Vue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this recursive Vue component render?
Consider a Vue 3 recursive component that renders a nested list of items. What will be the rendered output for the given data?
Vue
<template>
  <ul>
    <li v-for="item in items" :key="item.id">
      {{ item.name }}
      <RecursiveList v-if="item.children" :items="item.children" />
    </li>
  </ul>
</template>

<script setup>
import { defineProps } from 'vue'
const props = defineProps({ items: Array })
</script>
A<ul><li>Fruit<ul><li>Apple</li><li>Banana</li></ul></li><li>Vegetables</li><li>Carrot</li></ul>
B<ul><li>Fruit</li><li>Apple</li><li>Banana</li><li>Vegetables</li><li>Carrot</li></ul>
C<ul><li>Fruit<ul><li>Apple<ul></ul></li><li>Banana<ul></ul></li></ul></li><li>Vegetables<ul><li>Carrot<ul></ul></li></ul></li></ul>
D<ul><li>Fruit<ul><li>Apple</li><li>Banana</li></ul></li><li>Vegetables<ul><li>Carrot</li></ul></li></ul>
Attempts:
2 left
💡 Hint
Think about how the component calls itself only when children exist, nesting the lists.
📝 Syntax
intermediate
2:00remaining
Which option correctly registers a recursive component in Vue 3?
You want to create a recursive component named 'TreeNode' that calls itself inside its template. Which registration method is correct?
Vue
<template>
  <div>
    <TreeNode v-if="node.children" :node="child" v-for="child in node.children" :key="child.id" />
    <span>{{ node.name }}</span>
  </div>
</template>

<script setup>
import { defineProps } from 'vue'
const props = defineProps({ node: Object })
// How to register TreeNode recursively?
</script>
Aconst TreeNode = defineComponent({ name: 'TreeNode', /* no components registration needed */ })
Bconst TreeNode = defineComponent({ name: 'TreeNode', components: { 'TreeNode': TreeNode }, /* ... */ })
Cconst TreeNode = defineComponent({ name: 'TreeNode', components: { TreeNode }, /* ... */ })
Dconst TreeNode = defineComponent({ name: 'TreeNode', components: { TreeNode: () => import('./TreeNode.vue') }, /* ... */ })
Attempts:
2 left
💡 Hint
Vue 3 allows a component to call itself by name without explicit registration inside components.
🔧 Debug
advanced
2:00remaining
Why does this recursive Vue component cause a maximum call stack exceeded error?
Given this recursive component code, why does it crash with a maximum call stack exceeded error?
Vue
<template>
  <div>
    <RecursiveComp v-if="true" />
  </div>
</template>

<script setup>
import RecursiveComp from './RecursiveComp.vue'
</script>
AThe component is missing a key attribute in v-for causing infinite loops.
BThe component calls itself unconditionally without a base case to stop recursion.
CThe import statement causes circular dependency leading to stack overflow.
DVue 3 does not support recursive components by default.
Attempts:
2 left
💡 Hint
Think about what stops recursion in recursive components.
state_output
advanced
2:00remaining
What is the value of 'count' after clicking the button twice in this recursive Vue component?
This recursive component increments a count state on button click. What is the final count after two clicks?
Vue
<template>
  <div>
    <button @click="increment">Click me</button>
    <p>Count: {{ count }}</p>
    <RecursiveCounter v-if="depth < 2" :depth="depth + 1" />
  </div>
</template>

<script setup>
import { ref, defineProps } from 'vue'
const props = defineProps({ depth: { type: Number, default: 0 } })
const count = ref(0)
function increment() {
  count.value++
}
</script>
ACount is 2 because each component instance has its own count state.
BCount is 0 because the button does not trigger increment correctly.
CCount is 4 because clicks propagate to all recursive instances.
DCount is 1 because only the root component increments count.
Attempts:
2 left
💡 Hint
Each recursive component instance has its own separate state.
🧠 Conceptual
expert
3:00remaining
How does Vue 3 handle recursive component resolution internally?
Which statement best describes how Vue 3 resolves recursive components during rendering?
AVue 3 resolves recursive components only if they are imported dynamically with defineAsyncComponent.
BVue 3 forbids recursive components to prevent infinite loops at compile time.
CVue 3 uses the component's name to resolve itself recursively without explicit registration in components option.
DVue 3 requires recursive components to be globally registered to resolve recursion.
Attempts:
2 left
💡 Hint
Think about how Vue 3 improved recursive component support compared to Vue 2.