Chapter 23: Vue v-for Components

v-for with components in Vue 3.

This is one of the most common and most powerful patterns you’ll use in real Vue applications. Almost every real app has lists: todo items, products in a shop, users in a table, messages in chat, cards in a dashboard, search results, blog posts… and in 99% of cases you will not repeat the same HTML structure 20 times manually — you will create one reusable component and then loop it with v-for.

So “Vue v-for Components” really means:

Using v-for to render multiple instances of the same component, passing different data to each instance via props, and usually listening to events that each child component can emit back to the parent.

Let’s go through it step by step with a realistic, modern example.

Classic Pattern – Todo List with TodoItem Component

This is the pattern you’ll see in almost every intermediate/advanced Vue tutorial and real project.

1. Child Component: TodoItem.vue

vue

2. Parent Component – Using v-for to render many TodoItem

vue

Why This Pattern is So Powerful (Key Lessons)

  1. Reusability One TodoItem component → used 1 time or 1000 times — same code.
  2. Single source of truth All logic & state lives in parent (todos array) → child is dumb (just renders props & emits events).
  3. v-for + :key is mandatory:key=”todo.id” → Vue can track which item is which when list changes (add/delete/reorder) → prevents bugs.
  4. Props go down, events go up (“Data down, actions up”)
    • :text=”todo.text” → data flows down
    • @toggle=”toggleTodo” → child tells parent “toggle this id”
  5. TypeScript makes it safedefineProps<{ id: number; text: string; done: boolean }> → autocompletion, no typos

Quick Cheat Sheet – v-for + Components

Part What you write Purpose / Tip
Parent loop <TodoItem v-for=”todo in todos” :key=”todo.id” /> Always :key with unique stable value
Pass data :id=”todo.id” :text=”todo.text” :done=”todo.done” Use : for dynamic values (not strings)
Listen to child events @toggle=”toggleTodo” @delete=”deleteTodo” Child emits → parent handles state change
Child declares props defineProps<{ id: number; text: string; done: boolean }>() Best with TS
Child emits events defineEmits<{ (e: ‘toggle’, id: number): void }>() Type-safe events

Your Mini Homework

  1. Add an edit feature:
    • Add @edit emit in TodoItem (maybe double-click on text)
    • In parent → when @edit → prompt for new text → update todo
  2. Create a ProductCard.vue component and loop 6–8 fake products

Any part still unclear? Want me to show:

  • How to make TodoItem editable inline?
  • v-for with <TransitionGroup> for animations?
  • Passing functions as props (advanced, sometimes needed)?
  • Difference between v-for on component vs on native element?

Just say — we’ll build the next step together 🚀

Happy looping components from Hyderabad! 💙

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *