Chapter 75: Vue v-if Directive

V-if

This is the directive you reach for whenever you want to conditionally render (or completely remove) an element or block of template based on a JavaScript expression.

It’s not the same as v-show — and understanding the difference is what separates beginner code from clean, performant production code.

1. The Core Idea – What v-if Actually Does

v-if tells Vue:

“Only include this element (and everything inside it) in the real DOM if this expression is true. If it becomes false, completely destroy the element and all its children — remove it from the DOM.”

This is very different from just hiding something with CSS.

2. v-if vs v-show – The Most Important Comparison (Memorize This Table)

Feature v-if v-show When to choose which
How it works Adds/removes element from DOM Changes display: none / display: block —
Initial render cost Only renders when true Always renders (even when hidden) v-if wins if expensive
Re-render cost when toggling Full mount/unmount cycle Very cheap (just CSS toggle) v-show wins if frequent toggle
Lifecycle hooks on toggle onMounted / onUnmounted run every time Never — component stays mounted v-if = lifecycle reset
Animations Works perfectly with <Transition> Works, but element always exists Both good
Best for Content that rarely changes (modals, auth gates, heavy components) Content that toggles often (tabs, accordions, tooltips, loading spinners) —
DOM presence Gone when false Hidden but present v-if = cleaner DOM

Golden rule of thumb (2026)

  • Use v-if when:
    • The condition changes rarely (login status, user role, modal open/close)
    • The content is expensive to render (charts, maps, large lists, complex components)
    • You want true destruction (unmount → cleanup timers, event listeners, memory)
  • Use v-show when:
    • The condition changes very often (tab switching, accordion open/close, hover tooltips)
    • You want instant toggle (no mount/unmount cost)
    • The component should remember state (form values, scroll position, internal counters)

3. Real, Practical Example – Login Form with Conditional Content

vue

Key Lessons from This Example

  • v-if → completely removes login form when user is logged in → no unnecessary DOM, no hidden listeners
  • v-else-if → loading state only shown during initial check
  • v-else → default fallback when neither logged in nor loading
  • No extra wrapper <div> needed — Vue handles conditional blocks cleanly

Quick Reference Table – v-if Family (2026 Must-Know)

Directive When to use it DOM presence when false Re-mounts on true? Cost on toggle Animation support
v-if Condition changes rarely, heavy content Removed Yes Higher Perfect with <Transition>
v-else-if Chain of conditions Removed Yes Higher Yes
v-else Default fallback Removed Yes Higher Yes
v-show Condition changes frequently, light content Hidden (display:none) No Very low Works, but element always exists

Pro Tips from Real Projects (Hyderabad 2026)

  • Use v-if for:

    • Modals / dialogs (they’re either shown or completely gone)
    • Auth gates / role-based content
    • Heavy components (charts, maps, large tables)
    • Content that should not run code when hidden (timers, event listeners)
  • Use v-show for:

    • Tab content / accordions (toggle 100× a day)
    • Loading spinners / skeletons
    • Tooltips / dropdowns
    • Anything where you want instant toggle and state preservation
  • Always use <template v-if> when you want to conditionally render multiple elements without extra wrapper:

    vue
  • Combine v-if + <Transition> for beautiful enter/leave animations

  • Never use v-if on very frequently toggled content — performance hit from mount/unmount cycle

Your mini practice task:

  1. Create a component with:
    • v-if for logged-in vs login form
    • v-else-if for loading state
    • v-else for guest view
  2. Add <Transition name=”fade”> around the content
  3. Toggle login state → watch smooth fade + correct mount/unmount behavior

Any part confusing? Want full examples for:

  • v-if + <Transition> + modal animation?
  • v-if vs v-show performance comparison?
  • v-if inside <TransitionGroup> for list items?
  • Real auth guard with v-if + router?

Just tell me — we’ll build the next clean conditional UI together 🚀

You may also like...

Leave a Reply

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