Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Vue 3 debugging reference for reactivity issues, computed errors, watcher bugs, async failures, and SSR hydration problems.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
reference/v-for-component-props.md
1---2title: Always Pass v-for Data to Components via Props3impact: MEDIUM4impactDescription: Components have isolated scope - v-for iteration data is not automatically available inside child components5type: gotcha6tags: [vue3, v-for, components, props, list-rendering]7---89# Always Pass v-for Data to Components via Props1011**Impact: MEDIUM** - Vue components have isolated scope by design. When using `v-for` to render components, the iteration variable (e.g., `item`) is NOT automatically available inside the child component. You must explicitly pass the data through props.1213This isolation is intentional - it keeps components reusable and their data dependencies explicit. Forgetting to pass props results in `undefined` data or errors about missing properties.1415## Task Checklist1617- [ ] Always bind iteration data to component props explicitly18- [ ] Pass both the item and index if the component needs them19- [ ] Always include a unique `:key` when rendering components with v-for20- [ ] Define corresponding props in the child component to receive the data2122**Incorrect:**23```html24<!-- WRONG: Component cannot access 'todo' - it's not in scope -->25<TodoItem v-for="todo in todos" :key="todo.id" />2627<!-- Inside TodoItem.vue: this.todo or todo is undefined! -->28```2930```html31<!-- WRONG: Key provided but no data passed -->32<UserCard33v-for="user in users"34:key="user.id"35/>36<!-- UserCard has no user data to display -->37```3839**Correct:**40```html41<!-- CORRECT: Explicitly pass the item as a prop -->42<TodoItem43v-for="todo in todos"44:key="todo.id"45:todo="todo"46/>47```4849```html50<!-- CORRECT: Pass multiple pieces of data -->51<UserCard52v-for="(user, index) in users"53:key="user.id"54:user="user"55:index="index"56:is-first="index === 0"57/>58```5960```vue61<!-- Child component: UserCard.vue -->62<script setup>63defineProps({64user: {65type: Object,66required: true67},68index: {69type: Number,70required: true71},72isFirst: {73type: Boolean,74default: false75}76})77</script>7879<template>80<div class="user-card">81<span>{{ index + 1 }}. {{ user.name }}</span>82<span v-if="isFirst">(First User)</span>83</div>84</template>85```8687## Why Explicit Props?88891. **Clear data flow**: Makes dependencies explicit and traceable902. **Reusability**: Components work anywhere, not just inside specific v-for loops913. **Type safety**: Props can be validated with type and required checks924. **Maintainability**: Easier to understand what data a component needs9394## Reference95- [Vue.js List Rendering - v-for with Components](https://vuejs.org/guide/essentials/list.html#v-for-with-a-component)96