Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Directives  ›  Lesson

Conditional Rendering — v-if and v-show

Conditional Rendering 13 minv-if adds and removes elements based on a condition, while v-show keeps the element and only toggles its display.

Conditional Rendering — v-if and v-show

A new Shelf user has no books, so instead of an empty list they should see "Your shelf is empty — add your first book." Meanwhile the filter panel opens and closes every few seconds as people browse. Both are "show this sometimes", but they deserve different tools.

The rule

v-if builds or destroys; v-show only hides.

v-if renders an element only while its expression is truthy. When the expression turns falsy, the element, and any components inside it, are removed from the page entirely. Branches chain with v-else-if and v-else, which must sit on the element immediately after:

<p v-if="books.length === 0">Your shelf is empty — add your first book.</p>
<p v-else-if="books.length === 1">One book so far. Nice start.</p>
<BookList v-else :books="books" />

To switch a group of elements without adding a wrapper, put v-if on a <template> tag; the template itself is not rendered:

<template v-if="showStats">
  <h3>Stats</h3>
  <p>{{ finishedCount }} finished</p>
</template>

v-show always renders the element and toggles the CSS display property:

<aside v-show="filtersOpen" class="filters">...</aside>

Three limits of v-show: it cannot be used on <template>, it has no v-else partner, and the hidden element still exists in the page, so its components are created and keep running.

Cost trade-off. v-if is lazy: if the condition starts false, nothing is built. But every toggle tears down and rebuilds. v-show pays once, up front, then toggling is nearly free.

The shortcut

Ask "How often will this flip?" Rarely, or often false from the start → v-if. Many times while the user is on the page → v-show. Need an else branch → v-if.

Choosing for three parts of ShelfStep 1 of 4
  1. Empty-shelf message

    Shown once for new users, never flips back and forth, needs an else (the list). v-if / v-else.

A classic bug is slipping another element between a v-if and its v-else. The v-else then has no partner and Vue raises a compile error.

Drills

Answer fast — trust the rule.

quick drill

What happens to an element with v-show when its condition is false?

quick drill

Where must v-else be placed?

quick drill

A settings drawer is opened and closed dozens of times per visit. Which fits best?

quick drill

Which of these does NOT work?

Remember

  • v-if / v-else-if / v-else add and remove elements; v-show keeps them and toggles display.
  • Shortcut: flips often → v-show; rarely, or needs an else → v-if.
  • Trap: anything between v-if and v-else breaks the chain; v-show does not work on <template>.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

Explainv-if or v-show?

Shelf has an empty-shelf message that appears once when a new user has no books, and a filter panel users open and close many times a minute. Pick v-if or v-show for each and explain.