🔍 Vue v-if – Conditional Rendering in Vue.js (2025 Guide)
🧲 Introduction – What is v-if
in Vue?
When building interactive interfaces, you often need to show or hide elements based on data conditions. Vue.js offers powerful conditional rendering through the v-if
directive, letting you control DOM output dynamically and efficiently.
🎯 In this guide, you’ll learn:
- What
v-if
,v-else-if
, andv-else
do - When to use
v-if
vs.v-show
- How conditional blocks are handled under the hood
- Examples with full explanations
🧩 What is the v-if
Directive?
v-if
is a directive that conditionally adds or removes elements from the DOM based on an expression. If the condition is false, Vue does not render the element at all.
✅ Basic Syntax:
<p v-if="isVisible">This is visible</p>
🧠 Explanation:
- The paragraph only appears if
isVisible
istrue
- If
false
, Vue removes the entire element from the DOM
🧪 Example – Simple Conditional Display
<template>
<div>
<button @click="toggle">Toggle Info</button>
<p v-if="showInfo">This is some extra info.</p>
</div>
</template>
<script>
export default {
data() {
return { showInfo: true };
},
methods: {
toggle() {
this.showInfo = !this.showInfo;
}
}
}
</script>
✅ What Happens:
- Clicking the button toggles
showInfo
v-if
re-renders the<p>
only whenshowInfo
istrue
🔁 Using v-else-if
and v-else
You can chain conditions like an if-else-if-else
block:
<p v-if="score >= 90">Grade: A</p>
<p v-else-if="score >= 75">Grade: B</p>
<p v-else>Grade: C or below</p>
🧠 Explanation:
- Vue evaluates from top to bottom
- Only the first true condition is rendered
- The rest are ignored
🆚 v-if vs. v-show – What’s the Difference?
Directive | DOM Behavior | Use Case |
---|---|---|
v-if | Completely removes/adds from DOM | Expensive or rarely toggled content |
v-show | Just toggles display: none | Frequently toggled visibility |
⚠️ Performance Tip:
- Use
v-if
when the element won’t change often - Use
v-show
for elements that toggle frequently
🧬 v-if with v-for
– Use with Caution
Vue recommends avoiding v-if
on the same element as v-for
. If needed, wrap one inside another:
<ul>
<li v-for="item in items" v-if="item.visible" :key="item.id">{{ item.name }}</li>
</ul>
⚠️ This renders only items
with visible: true
, but it can reduce performance if the list is long.
📘 Real-World Example – Login Conditional Rendering
<template>
<div>
<p v-if="user">Welcome, {{ user.name }}</p>
<p v-else>Please log in to continue.</p>
</div>
</template>
<script>
export default {
data() {
return { user: null };
}
}
</script>
🧠 Use Case: Show personalized content if user is authenticated.
📌 Summary – Recap & Next Steps
The v-if
directive gives you granular control over what appears in your Vue components. Unlike v-show
, it ensures that only relevant DOM elements are generated, improving performance when used correctly.
🔍 Key Takeaways:
v-if
adds/removes elements based on truthy conditions- Use
v-else-if
andv-else
for fallback options - Use
v-show
for fast visibility toggling,v-if
for heavy DOM content
⚙️ Real-World Relevance:
Used in modals, alerts, loaders, authentication, and conditional UIs across almost every Vue app.
❓ FAQ Section
❓ What does v-if
do in Vue?
✅ It conditionally renders an element based on a Boolean expression. If false, the element is removed from the DOM.
❓ How is v-if
different from v-show
?
✅ v-if
controls whether the element exists in the DOM, while v-show
toggles visibility with CSS (display: none
).
❓ Can I use v-if
with v-for
?
✅ Yes, but it’s best to wrap them:
<ul>
<template v-for="item in items">
<li v-if="item.visible">{{ item.name }}</li>
</template>
</ul>
❓ Does Vue cache v-if
elements?
✅ No. Vue recreates them when the condition becomes true
again unless you use v-once
or <keep-alive>
for components.
❓ What’s the best practice for conditionally showing content?
✅ Use v-if
when content is heavy or rarely shown, and v-show
when toggling frequently (e.g. tabs or dropdowns).
Share Now :