Chapter 5: Vue v-if

What does v-if actually do?

v-if is Vue’s way of saying: “Only create this HTML element (and everything inside it) in the DOM if my condition is truthy. If false → don’t even render it at all.”

Key points right away:

  • It’s not just hiding something (that’s v-show)
  • When condition = false → the element is completely removed from the DOM (not even in devtools)
  • When condition becomes true → Vue creates the element + compiles it + mounts any child components
  • When it flips back to false → Vue destroys it (unmounts components, removes event listeners, etc.)

This makes v-if perfect for:

  • Authentication checks (admin panel vs guest view)
  • Lazy-loading heavy sections (modals, charts, maps)
  • Performance-critical parts that you don’t want rendered until needed

Official Vue 3 docs say:

Conditionally render an element or a template fragment based on the truthy-ness of the expression value.

1. Basic Example – Single v-if

vue

→ Open devtools → Elements tab When isLoggedIn = false → no <h2> exists in DOM at all When true → it appears instantly.

2. v-if + v-else-if + v-else Chain

Just like JavaScript if / else if / else

vue

Rules:

  • v-else and v-else-ifmust immediately follow a v-if or v-else-if (no other element between)
  • You can chain as many v-else-if as you want

3. The Most Important Pattern: v-if on <template>

Because v-if must be on one element, but often you want to conditionally render multiple elements or just text nodes.

Use <template v-if> — it acts as an invisible wrapper (doesn’t create extra DOM node)

vue

→ No useless <div> wrapper in DOM when modal is closed

4. v-if vs v-show – The Big Comparison (you’ll be asked this in interviews)

Feature v-if v-show
DOM presence Removed completely when false Always in DOM, just display: none
Initial render (false) Very fast – nothing rendered Slower – renders everything
Toggling cost Expensive (create/destroy components) Very cheap (just CSS toggle)
Best for Rarely changing conditions, heavy content Frequently toggled UI (tabs, accordions)
Lifecycle hooks mounted/unmounted fire each time Always mounted once
Use with <transition> Great (enter/leave animations) Also works, but element always exists
Memory usage (hidden) Low (no nodes) Higher (nodes + reactivity)

Real 2026 rule-of-thumb:

  • Use v-if for: modals, admin sections, expensive charts, anything behind auth
  • Use v-show for: tabs, dropdown menus, loading spinners, frequent toggles

Advanced combo (for expensive components toggled often):

vue

→ v-if prevents initial render until needed → v-show prevents destroy/re-create on tab switch

5. Common Gotchas & Pro Tips (2026 edition)

  • Don’t put v-if and v-for on the same element (Vue 3 priority changed → v-if evaluated first → often breaks). Use <template v-for> + inner v-if or computed filter.
  • Complex conditions? → Move to computed
JavaScript
HTML
  • v-if with async data (fetch)
vue
  • Truthy/falsy reminder: 0, ”, null, undefined, false → falsy

Summary Table – When to Choose v-if

Scenario Prefer v-if? Why?
Modal / dialog Yes Heavy, infrequent
User role-based UI (admin vs user) Yes Security + perf
Loading / error / success states Yes Clean DOM
Tab content that switches 100×/day Usually v-show Cheap toggle
Initial page load with false condition Yes Faster first paint

Practice challenge: Build a small dashboard with:

  • Login form (v-if=”!loggedIn”)
  • Dashboard content (v-if=”loggedIn”)
  • Loading spinner (v-if=”loading”)
  • Error alert (v-else-if=”error”)

Any part you want deeper?

  • How v-if works with <Transition> for animations?
  • v-if inside v-for pitfalls?
  • Real project example with Pinia auth state?

Just tell me — we’ll keep going 🚀

Happy conditional rendering from Hyderabad! 🌶️💻

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *