Skip to content

v-if vs v-show

v-if truly creates/destroys nodes; v-show toggles CSS display. This lesson covers practical patterns, syntax, and mistakes to avoid.

Conditional Rendering Directives

v-if truly creates/destroys nodes; v-show toggles CSS display.

Prefer v-if for rarely shown content; v-show for frequent toggles.

<p v-if="error">{{ error }}</p>
<p v-show="isOpen">Drawer</p>

Contrast expensive vs cheap toggles.

Also

`v-else`, `v-else-if`, `<template v-if>`
  • Prefer Vue 3 + Composition API.
  • Use script setup for SFCs.
  • Keep components focused.
  • Lean on official docs.

v-if vs v-show Cheatsheet

Quick reference for patterns covered in this lesson.

Item Example Purpose
v-if lazy mount Truth
v-show CSS Frequent
v-else chain Branches
template group No element
keys reuse control Force
a11y presence Consider

Cost

Initial v-show renders hidden content — beware heavy trees.

Common Mistakes

  • v-if/v-for on same node.
  • Using v-show for permission-sensitive content that shouldn't exist.

Key Takeaways

  • v-if vs v-show is a core Vue skill.
  • Practice in a Vite app.
  • Prefer clarity.
  • Revisit docs for edge cases.

Pro Tip

For auth-gated UI, prefer v-if so secrets aren't just display:none.