Bird
Raised Fist0
Vueframework~20 mins

Recursive components in Vue - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of a recursive component in Vue?
easy
A. To handle user input events more efficiently
B. To create multiple unrelated components in one file
C. To optimize component rendering speed by caching
D. To allow a component to call itself to render nested or hierarchical data

Solution

  1. Step 1: Understand recursive component concept

    A recursive component is designed to call itself to handle nested or hierarchical data structures like trees or nested menus.
  2. Step 2: Compare options with definition

    Only To allow a component to call itself to render nested or hierarchical data correctly describes this behavior. Other options describe unrelated concepts.
  3. Final Answer:

    To allow a component to call itself to render nested or hierarchical data -> Option D
  4. Quick Check:

    Recursive component = self-calling component [OK]
Hint: Recursive means component calls itself for nested data [OK]
Common Mistakes:
  • Confusing recursion with event handling
  • Thinking recursive means multiple components in one file
  • Assuming recursion is about performance optimization
2. Which of the following is the correct way to name a recursive component in Vue 3 so it can call itself?
easy
A. const TreeNode = defineComponent({ components: { TreeNode }, ... })
B. const TreeNode = defineComponent({ setup() { return {}; } })
C. const TreeNode = defineComponent({ name: 'TreeNode', ... })
D. const TreeNode = defineComponent({ recursive: true, ... })

Solution

  1. Step 1: Identify how to enable self-reference

    Vue requires a component to have a name property to call itself recursively by that name.
  2. Step 2: Check options for correct naming

    const TreeNode = defineComponent({ name: 'TreeNode', ... }) correctly sets the name property. const TreeNode = defineComponent({ setup() { return {}; } }) lacks a name, C incorrectly tries to register itself as a child, and D uses a non-existent property.
  3. Final Answer:

    const TreeNode = defineComponent({ name: 'TreeNode', ... }) -> Option C
  4. Quick Check:

    Set component name for recursion [OK]
Hint: Name the component to enable self-reference [OK]
Common Mistakes:
  • Omitting the name property
  • Trying to register the component inside itself
  • Using invalid properties like 'recursive'
3. Given this recursive component snippet, what will be rendered if node has two children?
<template>
  <div>{{ node.name }}
    <TreeNode v-for="child in node.children" :key="child.id" :node="child" />
  </div>
</template>
medium
A. The component renders the node's name and recursively renders each child node inside nested divs
B. Only the root node's name is rendered, children are ignored
C. An infinite loop occurs causing a crash
D. The component renders the node's name but children are rendered as plain text

Solution

  1. Step 1: Understand recursive rendering logic

    The component renders the current node's name, then for each child, it calls itself recursively, rendering nested divs for children.
  2. Step 2: Analyze output for two children

    With two children, the component renders the root node's name, then two nested TreeNode components, each rendering their own names and children recursively.
  3. Final Answer:

    The component renders the node's name and recursively renders each child node inside nested divs -> Option A
  4. Quick Check:

    Recursive call renders nested children [OK]
Hint: Recursive components render nested children inside themselves [OK]
Common Mistakes:
  • Assuming children are ignored
  • Thinking recursion causes infinite loop here
  • Expecting children as plain text instead of components
4. What is the main issue with this recursive component code?
<template>
  <div>{{ node.name }}
    <TreeNode v-for="child in node.children" :node="child" />
  </div>
</template>

<script>
export default {
  name: 'TreeNode',
  props: ['node']
}
</script>
medium
A. Props are not declared correctly, should use props: { node: Object }
B. Missing :key on v-for causing rendering warnings
C. The component lacks a name property for recursion
D. Recursive call will cause infinite loop without a base case

Solution

  1. Step 1: Check for common recursive component issues

    The code correctly names the component and declares props. The recursive call is valid.
  2. Step 2: Identify missing key in v-for

    Vue requires a unique :key on elements inside v-for for efficient rendering. This is missing, causing warnings.
  3. Final Answer:

    Missing :key on v-for causing rendering warnings -> Option B
  4. Quick Check:

    Always add :key in v-for loops [OK]
Hint: Always add :key in v-for to avoid warnings [OK]
Common Mistakes:
  • Thinking name property is missing
  • Believing props declaration is invalid
  • Assuming infinite loop without base case
5. You want to display a nested comment thread using a recursive component. Each comment may have zero or more replies. Which approach correctly prevents infinite recursion and handles empty children arrays?
hard
A. In the template, use <Comment v-for="reply in comment.replies" :key="reply.id" :comment="reply" /> and ensure comment.replies defaults to an empty array if no replies
B. Render all comments without checking children, assuming replies always exist
C. Use a v-if to stop recursion when comment.replies is undefined, but do not default to empty array
D. Avoid recursion and flatten the comment tree before rendering

Solution

  1. Step 1: Understand recursion base case

    To prevent infinite recursion, the component must handle cases where there are no children by using an empty array or conditional rendering.
  2. Step 2: Analyze options for best practice

    In the template, use <Comment v-for="reply in comment.replies" :key="reply.id" :comment="reply" /> and ensure comment.replies defaults to an empty array if no replies uses v-for with a key and ensures replies default to an empty array, preventing errors and infinite loops. Render all comments without checking children, assuming replies always exist ignores empty children, C risks errors without default, and A avoids recursion but is not recursive component usage.
  3. Final Answer:

    Use v-for with key and default empty array for replies -> Option A
  4. Quick Check:

    Default empty array stops infinite recursion [OK]
Hint: Default children to empty array to stop recursion errors [OK]
Common Mistakes:
  • Not providing default empty array for children
  • Assuming children always exist
  • Avoiding recursion when it's the best fit
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard