Chapter 73: Vue v-for Directive

V-for

This is the directive you use to loop over arrays or objects and render a block of template for each item — exactly like a for…of loop in JavaScript, but declaratively inside HTML.

Without v-for, you would have to write the same <li>, <div class=”card”>, <tr>, or <option> tag 10, 20, 100 times manually. With v-for, you write it once and let Vue repeat it for you — and it stays reactive: when the array changes (add, remove, reorder), Vue intelligently updates only the necessary parts of the DOM.

1. Basic Syntax – The Golden Rule You Must Never Break

HTML

The #1 unbreakable rule in Vue 3 (2026)

You must always provide a :key attribute on the element that has v-for.

  • :key must be unique within the current list
  • :key must be stable — the same item must always have the same key value
  • :key should be a primitive (string or number) — never an object

2. Why :key is mandatory – The bugs you will see without it

Bug 1 – Input focus jumps / lost when deleting items

HTML

→ Type in the second input → delete the first item → focus jumps to the wrong input or disappears → Reason: Vue re-uses DOM elements based on position → the second <input> becomes the first one

Bug 2 – Animations break or look wrong

When using <TransitionGroup> without :key → items don’t animate correctly on reorder/delete

Bug 3 – State of child components gets mixed up

HTML

→ Without :key, when list order changes → child components are reused in wrong order → internal state (checkboxes, form values) jumps to wrong item

Correct version – always with :key

HTML

→ Delete first item → focus stays on the correct input → Reorder → child state stays with the correct item

3. Real, Practical Example – Todo List with Filtering, Sorting & Animations

vue

Quick Summary Table – v-for Essentials (2026)

Feature Syntax / Rule Why it’s critical
Basic loop v-for=”item in items”
With index v-for=”(item, index) in items” Useful for numbering
With key (MANDATORY) :key=”item.id” Reactivity, animations, performance
Loop over object v-for=”(value, key) in object” Rendering object properties
Use <template> for no wrapper <template v-for=”…”> … </template> Clean DOM
Inside <TransitionGroup> :key + tag=”ul” or tag=”div” Animations work
Never use index as key :key=”index” → only for static lists Breaks on reorder/delete

Pro Tips from Real Projects (Hyderabad 2026)

  • Always use :key — Vue shows console warning if missing
  • Prefer database ID / UUID for :key — most stable
  • For local-only lists → Date.now() or incremental counter is fine
  • Use <template v-for> when you don’t want extra wrapper element
  • For animations → always pair with <TransitionGroup> + :key
  • Never mutate array with methods that break reactivity (push is fine, but items.length = 0 breaks → use items.value = [])

Your mini homework:

  1. Build the todo list above
  2. Add sorting & filtering → watch how smoothly items move with correct :key
  3. Remove :key → see broken animations & lost focus
  4. Add <template v-for> for a group of elements without wrapper

Any part confusing? Want full examples for:

  • v-for + <TransitionGroup> + drag-and-drop?
  • v-for on object + index/key/value?
  • v-for inside <select> for options?
  • Common bug with :key=”index” + inputs?

Just tell me — we’ll build the next perfect list together 🚀

You may also like...

Leave a Reply

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