Thuta Learning
Vue.js
BasicWeb DevelopmentBeginner

Conditional and List Rendering

Vue.jsLesson 720 min

What you'll walk away with

  • Choose between v-if and v-show
  • Add a stable key to v-for

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.

Mental model
  1. props or event
  2. component and template
  3. rendered Vue UI

Choose between v-if and v-show

Core syntax or pattern

vue
<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

  1. 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.

  2. 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.

  3. rendered Vue UI</template>

    Apply Conditional and List Rendering in a small example and test normal, edge, and failure cases.

Execution flow
  1. props or event
  2. component and template
  3. 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.

vue
<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>
You should see
✓ Read Vue guide
○ Build a component

Try 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
vue
<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

What best describes the core idea of Conditional and List Rendering?

Quick quiz 2 / 3

Which mistake should you avoid first?

Quick quiz 3 / 3

How should this be applied in a real project?

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

Easy traps

  • Using the list index as the key
  • Using v-if and v-for together on the same element

Exercise

Show only the products from the list with stock > 0, using each item's unique id as the key.

You'll know it worked when: ✓ Read Vue guide ○ Build a component

Updated on

Progress is saved only in this browser on this device. It isn't synced to other devices.

Report a problem with this lessonOpens your email app with this lesson's address filled in — nothing is sent until you send it.

Conditional and List Rendering | Thuta Learning