Vue Tip: When to Use v-if

Michael Hoffmann
Jul 20, 2022
| 1 min read
| 422 views
Michael Hoffmann
Jul 20, 2022
1 min read
| 422 views
Template

During toggles, v-if
entirely creates and destroys the element, including event listeners and child components:
1
<template>
2
<MyComponent v-if="showComponent" />
3
</template>
v-show
creates the element and only toggles its visibility using CSS.
1
<template>
2
<MyComponent v-show="showComponent" />
3
</template>
Info
Generally speaking, v-if has higher toggle costs while v-show has higher initial render costs. So prefer v-show if you need to toggle something very often and v-if if the condition is unlikely to change at runtime.
If you liked this Vue tip, follow me on Twitter to get notified about new tips, blog posts, and more. Alternatively (or additionally), you can subscribe to my weekly Vue newsletter:
New Vue & Nuxt tips delivered to your inbox:
Vue Tip: Avoid Empty Wrapper for ConditionsJul 14, 2022
Vue Tip: Use Vue App Instance as Global StoreJul 25, 2022
Show comments