Explain it simply
Conditional and List Rendering, in plain language
v-if removes an element from the DOM entirely when the condition is false. v-show only toggles CSS display, which makes it a better fit for UI that's toggled frequently. Always give every v-for list a unique :key.
Problem → Why → Solution
The problem — why this concept matters
Using the list index as the key
A common mistake
Using v-if and v-for together on the same element
Core concept — understand how it works
Use v-if, v-show, and v-for to render UI based on conditions and collections.
- props or event
- component and template
- rendered Vue UI
Choose between v-if and v-show
Core syntax or pattern
<script setup>
const tasks = [
{ id: 1, title: 'Read Vue guide', done: true },
{ id: 2, title: 'Build a component', done: false }
]
</script>
<template>
<p v-if="tasks.length === 0">No tasks</p>
<ul v-else>
<li v-for="task in tasks" :key="task.id">
{{ task.done ? '✓' : '○' }} {{ task.title }}
</li>
</ul>
</template>Use v-if, v-show, and v-for to render UI based on conditions and collections.
Read the code step by step
- props or event
<script setup>v-if removes an element from the DOM entirely when the condition is false. v-show only toggles CSS display, which makes it a better fit for UI that's toggled frequently. Always give every v-for list a unique :key.
- component and template
<p v-if="tasks.length === 0">No tasks</p>Use v-if, v-show, and v-for to render UI based on conditions and collections.
- rendered Vue UI
</template>Apply Conditional and List Rendering in a small example and test normal, edge, and failure cases.
- props or event
- component and template
- rendered Vue UI
Success
Choose between v-if and v-show
Failure
Using the list index as the key
Let's break it down simply
v-if removes an element from the DOM entirely when the condition is false. v-show only toggles CSS display, which makes it a better fit for UI that's toggled frequently. Always give every v-for list a unique :key.
<script setup>
const tasks = [
{ id: 1, title: 'Read Vue guide', done: true },
{ id: 2, title: 'Build a component', done: false }
]
</script>
<template>
<p v-if="tasks.length === 0">No tasks</p>
<ul v-else>
<li v-for="task in tasks" :key="task.id">
{{ task.done ? '✓' : '○' }} {{ task.title }}
</li>
</ul>
</template>✓ Read Vue guide
○ Build a componentTry it yourself
Show only the products from the list with stock > 0, using each item's unique id as the key.
List Rendering — Vue.js
Real-world usage
How to apply it in a real project
Apply Conditional and List Rendering in a small example and test normal, edge, and failure cases.
Mini challenge
Build it yourself
Show only the products from the list with stock > 0, using each item's unique id as the key.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
<script setup>
const tasks = [
{ id: 1, title: 'Read Vue guide', done: true },
{ id: 2, title: 'Build a component', done: false }
]
</script>
<template>
<p v-if="tasks.length === 0">No tasks</p>
<ul v-else>
<li v-for="task in tasks" :key="task.id">
{{ task.done ? '✓' : '○' }} {{ task.title }}
</li>
</ul>
</template>Expected: Choose between v-if and v-show
Show hint
Apply Conditional and List Rendering in a small example and test normal, edge, and failure cases.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- props or event → component and template → rendered Vue UI
- Use v-if, v-show, and v-for to render UI based on conditions and collections.
- Using the list index as the key
- Apply Conditional and List Rendering in a small example and test normal, edge, and failure cases.
List Rendering — Vue.js