Skip to content
Tutorials Center
My learning

Topics

Lesson 9 of 87 ยท Template Syntax

Conditional Rendering

Show and hide content in Vue 3 with v-if, v-else-if, v-else and v-show. Learn the template wrapper, how the two approaches differ and the cost of each.

An interface is rarely one fixed picture. A login form turns into a welcome message, a spinner gives way to a table, an error box shows up only when something failed. Vue handles this with directives you write right in the template: v-if, v-else-if, v-else and v-show.

The first three add and remove elements from the page. v-show keeps the element and only changes its visibility with CSS. Both approaches are correct in different situations, and this lesson shows how to tell them apart.

v-if, v-else-if and v-else

v-if="expression" renders its element only when the expression is truthy. When it turns falsy, Vue removes the element from the DOM. A v-else-if or v-else element must directly follow a v-if or v-else-if sibling, like the branches of an ordinary if statement:

A status message with three branches

Only one of the three paragraphs exists at a time. If you inspect the page while dragging the slider, you will see the elements being swapped, not hidden.

Because the branches are siblings that follow each other, you cannot put other elements between them. Vue warns when a v-else has no matching v-if right before it.

flowchart TD
  A["battery changes"] --> B{"battery > 60?"}
  B -- yes --> C["render first p"]
  B -- no --> D{"battery > 20?"}
  D -- yes --> E["render second p"]
  D -- no --> F["render third p"]

A v-if chain works like if / else if / else: exactly one branch is rendered.

Conditions on several elements with <template>

v-if goes on a single element. To switch a group of elements on and off without adding a wrapper <div> to your DOM, wrap them in a <template> tag and put the directive on it. The <template> element itself is not rendered; only its content is:

Grouping with a template wrapper

The <ul> contains three <li> items or one, with no extra wrapper element that would be invalid HTML inside a list. v-else also works on a <template>.

<template v-show> is not supported, because there is no real element to hide. Use v-if with a template, or v-show on a real element.

v-show

v-show="expression" always renders the element, and toggles its CSS display property. When the expression is false, Vue sets display: none on the element:

v-if versus v-show in the DOM

With open false, pressing "Count them" reports 1: the v-show panel is in the DOM (hidden), the v-if panel is not. After you toggle to true it reports 2.

Some limits to remember. v-show has no v-else; if you need an either-or, use v-if/v-else. And because it only sets display: none inline, it cannot make an element visible if your own CSS adds !important.

Which one should you use?

The two have different costs:

  • v-if is "real" conditional rendering. When it turns false, the element and everything inside it, including child components, is destroyed: event listeners are removed, lifecycle hooks such as onUnmounted run and local state is lost. When it turns true again, everything is created from scratch. This makes v-if cheap to keep around when it is rarely shown, since nothing is rendered up front, but expensive to toggle often.
  • v-show always pays the initial render cost (the element is created even if hidden) but toggling is almost free, just a CSS change. Child components stay alive and keep their state.

So prefer v-show for something the user flips back and forth (a dropdown, a tab body, a collapsible panel) and v-if when the condition rarely changes, when you want a clean slate on each show, or when the content should not exist at all (an element a user is not allowed to see, or a heavy component you may never need). Do not choose v-show as a security measure: hidden elements are still in the page source.

If you are unsure, start with v-if. Switch to v-show only when toggling is frequent and you can measure that re-creating the content is costly. Many small interfaces never need v-show at all.

v-if with v-for

You can combine them, but the rules are subtle (v-if is evaluated before v-for in Vue 3, so it cannot see the loop variable when both are on the same element). The recommended solution is covered in List Rendering with v-for.

Reusing elements and the key

Vue updates the DOM by patching. When the new virtual tree has an element of the same type in the same place, Vue reuses the existing DOM element and only changes what differs. That is fast, and normally invisible, but it surprises people with form inputs.

Good news first: in Vue 3 the branches of a v-if / v-else chain are given automatic unique keys by the compiler, so two different branches are never patched into each other. The reuse problem appears when one element stays in place and only its bindings change. A key forces Vue to throw the element away and create a fresh one when the key changes:

Element reused, then forced fresh with key

Type something into both inputs and press the button. The text in the first input stays, because Vue reused the same <input> and only changed its placeholder. The second input is cleared, because the changed key tells Vue to treat it as a different element. Use key whenever the user's typed state must not carry over.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does v-if do to the element when its expression becomes false?

    v-if renders conditionally: the element and its children are destroyed. Setting display: none is what v-show does.
  2. 2. You need a dropdown that the user opens and closes many times a second. Which directive is usually better?

    v-show has a higher initial cost but very cheap toggling. v-if would destroy and rebuild the dropdown on each change.
  3. 3. How do you show or hide three sibling elements together without adding a wrapper element?

    A template tag with v-if is not rendered itself, only its content. v-show does not work on template.
  4. 4. What must be true of an element carrying v-else?

    v-else has no expression of its own: it relies on the previous sibling in the chain, and Vue warns when there is none.

Summary

  • v-if, v-else-if and v-else add and remove elements; the branches must be adjacent siblings.
  • A <template v-if> switches a group of elements without an extra wrapper in the DOM.
  • v-show keeps the element and toggles display: none; it works on real elements only and has no v-else.
  • v-if is cheaper when a condition rarely changes, v-show when it flips often.
  • Vue 3 keys v-if branches automatically; add your own key when one element stays in place but must be recreated, for example an input whose typed text should reset.

Next, repeat elements for each item in your data with List Rendering with v-for.