跪拜 Guibai
← Back to the summary

Vue3's v-if vs v-show: Rendering Internals, Branch Pitfalls, and the Cost of Each Toggle

Preface:

In everyday Vue3 development, conditional rendering is the most frequently used directive combination: v-if / v-else-if / v-else / v-show.

The seemingly simple "show/hide" is a hot interview topic and a hotspot for production performance bugs:

Many developers only know how to use these directives without understanding the underlying rendering mechanism, leading to unexplained white screens, DOM flicker, performance lag, and branch matching errors in projects.

This article shares the complete Vue3 conditional rendering toolkit, covering: syntax rules, rendering principles, priority, differences, pitfalls, and best practices.

I. Basic usage of the four conditional directives:

1. v-if / v-else-if / v-else multi-branch rendering

v-if is true conditional rendering: when the condition is false, the DOM is destroyed directly and does not exist in the page.

It supports multi-branch sequential matching, matching from top to bottom, stopping at the first hit.

Basic example

<template>
  <div>
    <p v-if="score >= 90">Excellent</p>
    <p v-else-if="score >= 60">Pass</p>
    <p v-else>Fail</p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const score = ref(85)
</script>

Key hard syntax rules

  1. v-else-if / v-else must immediately follow the previous branch — no DOM, comment, or tag can be inserted in between, or it fails.
  2. Multi-branch matching is top-down, rendering only the first node that satisfies the condition.
  3. When the initial condition is false, the corresponding DOM is not created and not rendered.

2. v-show show/hide

v-show is just a CSS toggle: it controls visibility via display: none, and the DOM always exists.

Basic example

<p v-show="isShow">I am v-show content</p>

<script setup>
const isShow = ref(false)
</script>

II. Comparison of v-if and v-show underlying rendering principles:

1. v-if principle (lazy rendering)

Essence: DOM creation / destruction toggling

2. v-show principle (lazy loading, high-frequency toggling)

Essence: CSS style switching

III. Core differences between v-if and v-show

Comparison dimension v-if v-show
Control principle Destroy / create DOM Toggle display style
Initial render cost Low (false means no render) High (always renders regardless of true/false)
Toggle cost Very high (DOM rebuild) Very low (style change only)
Lifecycle Toggling triggers mounted/unmounted No lifecycle triggered
Template support Supports <template> Does not support <template>
Lazy rendering Supported Not supported
Use case Condition rarely changes, one-time judgment Condition toggles frequently (modals, tab switching)

One-sentence selection rule

IV. High-frequency pitfalls with v-else / v-else-if:

Pitfall 1: Inserting content between branches makes v-else fail

Wrong code:

<!-- ❌ div inserted in the middle, branch broken, v-else fails -->
<p v-if="flag">Show 1</p>
<div>I'm separator content</div>
<p v-else>Show 2</p>

Reason:

v-else must immediately follow v-if / v-else-if — as soon as any node appears in between, the branch chain breaks and parsing fails.

Correct approach:

<!-- ✅ Branches adjacent -->
<p v-if="flag">Show 1</p>
<p v-else>Show 2</p>

Pitfall 2: Writing multi-branch conditions in the wrong order, logic never executes:

<!-- ❌ Logic error: the excellent branch is never reached -->
<p v-if="score >= 60">Pass</p>
<p v-else-if="score >= 90">Excellent</p>

Matching is top-down, 85 >= 60 hits directly, the high-score branch never executes.

Multi-branches must be ordered from strict to loose!

Pitfall 3: Mixing v-if and v-for, poor performance + abnormal logic:

Common pitfall in Vue2 & Vue3

Absolutely forbidden to mix them directly!

Wrong example:

<!-- ❌ Highly disrecommended -->
<div v-for="item in list" v-if="item.age > 18"></div>

Correct optimization approaches:

  1. Filter in the data layer first (recommended)
const filterList = list.filter(item => item.age > 18)
  1. Use <template v-for> for outer loop, inner v-if judgment

V. v-show specific pitfalls:

Pitfall 1: v-show does not support template tag:

<!-- ❌ Invalid -->
<template v-show="isShow">
  <p>Content</p>
</template>

Because v-show relies on CSS control, template does not generate real DOM, so the style cannot be applied.

Pitfall 2: v-show hidden content still occupies JS resources:

v-show only makes things invisible — the DOM, component instance, timers, requests, and listeners are all still running. For modals or complex forms kept hidden for a long time, memory keeps being consumed.

VI. v-if exclusive advanced feature: template group rendering:

When you need to batch control visibility of a group of tags without an extra layer of unnecessary DOM nesting, use <template v-if>:

<template v-if="isShow">
  <h1>Title</h1>
  <p>Paragraph 1</p>
  <p>Paragraph 2</p>
</template>

Features:

VII. Best practice standards:

1. Preferred use cases:

2. Multi-branch standards:

3. Absolutely forbidden: