π Vue v-for in Components β Render Lists with Dynamic Components (2025 Guide)
π§² Introduction β What Is v-for
in Vue Components?
In Vue.js, v-for
is a directive that enables you to loop over arrays or objects and render lists of elements or components. Itβs especially powerful when used with components to dynamically render multiple instances, such as product cards, user profiles, or menu items.
π― In this guide, youβll learn:
- How to use
v-for
to render component lists - Pass props to each component instance
- Use
:key
for efficient updates - Best practices for loops inside templates
π What Is v-for
?
The v-for
directive repeats a block of template for each item in a data source, typically an array.
β Syntax:
<template v-for="item in items">
<!-- repeated block -->
</template>
π§± Basic List Rendering with Native Elements
<ul>
<li v-for="fruit in fruits" :key="fruit">{{ fruit }}</li>
</ul>
data() {
return {
fruits: ['Apple', 'Banana', 'Orange']
};
}
π§ Each fruit will render as an individual <li>
element.
π§© Rendering Components with v-for
Letβs say you want to render a custom component for each product in a list.
β
Step 1: Create a Component β ProductCard.vue
<template>
<div class="card">
<h2>{{ product.name }}</h2>
<p>${{ product.price }}</p>
</div>
</template>
<script>
export default {
props: ['product']
}
</script>
β
Step 2: Use v-for
to Render Component Instances
<ProductCard
v-for="item in products"
:key="item.id"
:product="item"
/>
data() {
return {
products: [
{ id: 1, name: 'Laptop', price: 899 },
{ id: 2, name: 'Phone', price: 499 }
]
};
}
π§ Vue renders one <ProductCard>
for each object in products
, passing item
as a prop.
π Why Use :key
with v-for
?
The :key
attribute helps Vue track element identity across updates. This improves performance and ensures stable DOM elements.
β
Always provide a unique and stable key
, such as item.id
.
π Looping with Index
You can access the index of the current loop:
<li v-for="(fruit, index) in fruits" :key="index">
{{ index + 1 }}. {{ fruit }}
</li>
β οΈ Prefer using a unique key over an index for dynamic lists.
𧬠Passing Multiple Props in a Loop
<UserCard
v-for="user in users"
:key="user.id"
:name="user.name"
:email="user.email"
/>
props: ['name', 'email']
π§ Each instance gets its own name and email from the parent loop.
π§ͺ Real-World Example β Todo List
β
TodoItem.vue
<template>
<li>
<input type="checkbox" :checked="todo.done" @change="$emit('toggle', todo.id)" />
{{ todo.text }}
</li>
</template>
<script>
export default {
props: ['todo']
}
</script>
β Parent Component
<ul>
<TodoItem
v-for="item in todos"
:key="item.id"
:todo="item"
@toggle="toggleTodo"
/>
</ul>
data() {
return {
todos: [
{ id: 1, text: 'Learn Vue', done: false },
{ id: 2, text: 'Build a project', done: true }
]
};
},
methods: {
toggleTodo(id) {
const todo = this.todos.find(t => t.id === id);
todo.done = !todo.done;
}
}
π§ This is a complete reactive loop with child-to-parent event handling.
π« Common Mistakes to Avoid
β Missing :key
in loops
β
Always include a unique :key
in v-for
lists
β Using index as key for dynamic data
β
Use item.id
or another unique identifier instead
β Mutating props inside the child
β
Emit events and handle data in the parent
π Summary β Recap & Next Steps
Using v-for
in Vue components allows you to dynamically render lists of reusable UI elements. Itβs perfect for modular, scalable app designβwhether youβre building a product catalog, blog post list, or todo app.
π Key Takeaways:
- Use
v-for
to loop over arrays or objects in templates - Combine
v-for
with components to render reusable UI blocks - Always use a unique
:key
for stable updates - Pass props and emit events to communicate between components
βοΈ Real-World Relevance:
This pattern is used everywhere in real Vue appsβcards, menus, modals, tables, user lists, and more.
β FAQ Section
β Can I use v-for
on components?
β Yes. Itβs commonly used to render dynamic component lists like:
<MyComponent v-for="item in list" :key="item.id" :data="item" />
β Why is the :key
important in v-for
?
β It helps Vue efficiently track DOM elements during updates. It prevents glitches in animations and state preservation.
β Can I use v-for
with v-if
on the same element?
β
Not recommended. Wrap v-if
inside the loop block instead:
<div v-for="item in list" :key="item.id" v-if="item.visible"></div>
β Can I emit events from components in a v-for
loop?
β Absolutely. Just pass a unique identifier and handle the event in the parent:
<ChildComp @delete="handleDelete(item.id)" />
β How do I access the loop index in v-for
?
β
Use (item, index)
:
<li v-for="(item, index) in items" :key="index">{{ index }}</li>
Share Now :