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:
- Why shouldn't
v-ifandv-forbe written together? - How exactly should you choose between
v-ifandv-show? - Why does
v-elseoccasionally fail to work? - Why does multi-branch conditional rendering easily go wrong?
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
v-else-if/v-elsemust immediately follow the previous branch — no DOM, comment, or tag can be inserted in between, or it fails.- Multi-branch matching is top-down, rendering only the first node that satisfies the condition.
- 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>
- Hidden state: DOM exists, style
display: none - Toggle cost is extremely low, no DOM destruction or rebuild involved
II. Comparison of v-if and v-show underlying rendering principles:
1. v-if principle (lazy rendering)
- Condition is false: no VNode created, no real DOM generated
- Condition changes false → true: VNode recreated, DOM rendered, node mounted
- Condition changes true → false: DOM destroyed, node removed, instance cleared
Essence: DOM creation / destruction toggling
2. v-show principle (lazy loading, high-frequency toggling)
- Regardless of true or false, the DOM is always initially rendered
- Visibility is toggled only by switching the CSS
displayproperty - No DOM destruction, no rebuild, no lifecycle execution
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
- Page loads and judges once, almost doesn't change → use v-if
- Frequent show/hide, toggling back and forth → use v-show
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
- Vue2:
v-forpriority >v-if, loops first then judges, extremely wasteful - Vue3:
v-ifpriority >v-for, judges first then loops, causing iteration to fail
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:
- Filter in the data layer first (recommended)
const filterList = list.filter(item => item.age > 18)
- Use
<template v-for>for outer loop, innerv-ifjudgment
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:
- No extra DOM node rendered
- Group destruction / creation
- v-show does not support this syntax
VII. Best practice standards:
1. Preferred use cases:
- Permission control, on-demand page module rendering, initial load judgments → v-if
- Modals, drawers, tab switching, high-frequency button visibility → v-show
2. Multi-branch standards:
- Strictly ensure
if / else-if / elseare adjacent with no gaps - Order conditions from high priority to low priority
- For complex multi-conditions, prefer
computedto unify state management
3. Absolutely forbidden:
- Forbidden: v-if and v-for mixed at the same level
- Forbidden: inserting unrelated nodes between v-else branches
- Forbidden: using v-if in high-frequency toggle scenarios