Recursive components let a component call itself to show nested or repeating data. This helps display things like trees or menus easily.
Recursive components in Vue
<script setup> import { defineProps } from 'vue' const props = defineProps({ item: Object }) </script> <template> <div> <p>{{ item.name }}</p> <ul v-if="item.children"> <li v-for="child in item.children" :key="child.id"> <RecursiveComponent :item="child" /> </li> </ul> </div> </template> <script> export default { name: 'RecursiveComponent' } </script>
The component must have a name option to refer to itself.
Use props to pass data down to each recursive call.
<template>
<div>
<p>{{ item.name }}</p>
<ul v-if="item.children">
<li v-for="child in item.children" :key="child.id">
<RecursiveComponent :item="child" />
</li>
</ul>
</div>
</template><template>
<div>
<p>{{ item.name }}</p>
<ul v-if="item.children && item.children.length">
<li v-for="child in item.children" :key="child.id">
<RecursiveComponent :item="child" />
</li>
</ul>
</div>
</template><template>
<div>
<p>{{ item.name }}</p>
<ul v-if="item.children">
<li v-for="child in item.children" :key="child.id">
<RecursiveComponent :item="child" />
</li>
</ul>
</div>
</template>This example shows a tree structure with nested children. The RecursiveComponent calls itself to render each child node. The indentation grows with each level.
<script setup> const treeData = { id: 1, name: 'Root', children: [ { id: 2, name: 'Child 1', children: [ { id: 3, name: 'Grandchild 1' }, { id: 4, name: 'Grandchild 2' } ] }, { id: 5, name: 'Child 2' } ] } </script> <template> <RecursiveComponent :item="treeData" /> </template> <script> export default { name: 'App', components: { RecursiveComponent: () => import('./RecursiveComponent.vue') } } </script>
Recursive components can cause infinite loops if data is circular. Always ensure data ends.
Time complexity depends on the number of nodes; each node is rendered once.
Use recursive components when data is naturally nested and unknown depth.
Recursive components let a component call itself to show nested data.
They are great for trees, menus, and nested comments.
Always pass data via props and name the component for self-reference.
Practice
recursive component in Vue?Solution
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.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.Final Answer:
To allow a component to call itself to render nested or hierarchical data -> Option DQuick Check:
Recursive component = self-calling component [OK]
- Confusing recursion with event handling
- Thinking recursive means multiple components in one file
- Assuming recursion is about performance optimization
Solution
Step 1: Identify how to enable self-reference
Vue requires a component to have anameproperty to call itself recursively by that name.Step 2: Check options for correct naming
const TreeNode = defineComponent({ name: 'TreeNode', ... })correctly sets thenameproperty.const TreeNode = defineComponent({ setup() { return {}; } })lacks a name, C incorrectly tries to register itself as a child, and D uses a non-existent property.Final Answer:
const TreeNode = defineComponent({ name: 'TreeNode', ... }) -> Option CQuick Check:
Set component name for recursion [OK]
- Omitting the name property
- Trying to register the component inside itself
- Using invalid properties like 'recursive'
node has two children?<template>
<div>{{ node.name }}
<TreeNode v-for="child in node.children" :key="child.id" :node="child" />
</div>
</template>Solution
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.Step 2: Analyze output for two children
With two children, the component renders the root node's name, then two nestedTreeNodecomponents, each rendering their own names and children recursively.Final Answer:
The component renders the node's name and recursively renders each child node inside nested divs -> Option AQuick Check:
Recursive call renders nested children [OK]
- Assuming children are ignored
- Thinking recursion causes infinite loop here
- Expecting children as plain text instead of components
<template>
<div>{{ node.name }}
<TreeNode v-for="child in node.children" :node="child" />
</div>
</template>
<script>
export default {
name: 'TreeNode',
props: ['node']
}
</script>Solution
Step 1: Check for common recursive component issues
The code correctly names the component and declares props. The recursive call is valid.Step 2: Identify missing key in v-for
Vue requires a unique:keyon elements insidev-forfor efficient rendering. This is missing, causing warnings.Final Answer:
Missing :key on v-for causing rendering warnings -> Option BQuick Check:
Always add :key in v-for loops [OK]
- Thinking name property is missing
- Believing props declaration is invalid
- Assuming infinite loop without base case
Solution
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.Step 2: Analyze options for best practice
In the template, use<Comment v-for="reply in comment.replies" :key="reply.id" :comment="reply" />and ensurecomment.repliesdefaults to an empty array if no replies usesv-forwith 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.Final Answer:
Use v-for with key and default empty array for replies -> Option AQuick Check:
Default empty array stops infinite recursion [OK]
- Not providing default empty array for children
- Assuming children always exist
- Avoiding recursion when it's the best fit
Ready to prove it?
Pass the test and this pattern moves unknown
