0
0
Vueframework~20 mins

v-show vs v-if difference in Vue - Practice Questions

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue Visibility Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What happens when using v-show on a Vue element?
Consider a Vue component with an element controlled by v-show. What is the visible effect when the condition is false?
Vue
<template>
  <div v-show="isVisible">Hello!</div>
</template>
<script setup>
import { ref } from 'vue'
const isVisible = ref(false)
</script>
AThe element is replaced with a comment node in the DOM.
BThe element remains in the DOM but is hidden with CSS <code>display: none</code>.
CThe element is removed from the DOM completely.
DThe element is visible but with zero opacity.
Attempts:
2 left
💡 Hint
Think about whether the element stays in the DOM or not.
component_behavior
intermediate
2:00remaining
What happens when using v-if on a Vue element?
Consider a Vue component with an element controlled by v-if. What happens to the element when the condition is false?
Vue
<template>
  <div v-if="isVisible">Hello!</div>
</template>
<script setup>
import { ref } from 'vue'
const isVisible = ref(false)
</script>
AThe element is removed from the DOM completely.
BThe element remains in the DOM but is hidden with CSS <code>display: none</code>.
CThe element is replaced with a transparent placeholder.
DThe element is visible but with zero opacity.
Attempts:
2 left
💡 Hint
Think about whether the element exists in the DOM or not.
state_output
advanced
2:00remaining
How many times is the mounted lifecycle hook called when toggling v-if?
Given a component with a child element using v-if to toggle visibility, how many times is the child's mounted hook called if the condition toggles from false to true and back to false and then true again?
Vue
<template>
  <ChildComponent v-if="show" />
  <button @click="show = !show">Toggle</button>
</template>
<script setup>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'
const show = ref(false)
</script>
AMultiple times continuously regardless of toggling.
BOnce, because the component stays mounted after first render.
CTwice, once each time the component is created when <code>show</code> becomes true.
DZero times, because <code>v-if</code> does not mount components.
Attempts:
2 left
💡 Hint
Remember what happens to components when v-if removes them.
state_output
advanced
2:00remaining
How many times is the mounted lifecycle hook called when toggling v-show?
Given a component with a child element using v-show to toggle visibility, how many times is the child's mounted hook called if the condition toggles from false to true and back to false and then true again?
Vue
<template>
  <ChildComponent v-show="show" />
  <button @click="show = !show">Toggle</button>
</template>
<script setup>
import { ref } from 'vue'
import ChildComponent from './ChildComponent.vue'
const show = ref(false)
</script>
AMultiple times continuously regardless of toggling.
BTwice, once each time the component is shown.
CZero times, because <code>v-show</code> does not mount components.
DOnce, because the component is mounted once and only hidden or shown.
Attempts:
2 left
💡 Hint
Think about whether the component is removed from the DOM or just hidden.
📝 Syntax
expert
3:00remaining
Which option correctly toggles visibility using v-if and v-show in Vue 3?
You want to toggle an element's visibility with a button. Which code snippet correctly uses v-if and v-show to show the element only when isVisible is true?
A
&lt;template&gt;
  &lt;div v-if="isVisible"&gt;Visible with v-if&lt;/div&gt;
  &lt;div v-show="isVisible"&gt;Visible with v-show&lt;/div&gt;
  &lt;button @click="isVisible = !isVisible"&gt;Toggle&lt;/button&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const isVisible = ref(false)
&lt;/script&gt;
B
&lt;template&gt;
  &lt;div v-if="isVisible"&gt;Visible with v-show&lt;/div&gt;
  &lt;div v-show="!isVisible"&gt;Visible with v-if&lt;/div&gt;
  &lt;button @click="isVisible = !isVisible"&gt;Toggle&lt;/button&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const isVisible = ref(false)
&lt;/script&gt;
C
&lt;template&gt;
  &lt;div v-if="!isVisible"&gt;Visible with v-if&lt;/div&gt;
  &lt;div v-show="!isVisible"&gt;Visible with v-show&lt;/div&gt;
  &lt;button @click="isVisible = !isVisible"&gt;Toggle&lt;/button&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const isVisible = ref(false)
&lt;/script&gt;
D
&lt;template&gt;
  &lt;div v-if="isVisible"&gt;Visible with v-show&lt;/div&gt;
  &lt;div v-show="isVisible"&gt;Visible with v-if&lt;/div&gt;
  &lt;button @click="isVisible = !isVisible"&gt;Toggle&lt;/button&gt;
&lt;/template&gt;
&lt;script setup&gt;
import { ref } from 'vue'
const isVisible = ref(false)
&lt;/script&gt;
Attempts:
2 left
💡 Hint
Check the directives and their conditions carefully.