0
0
Vueframework~20 mins

Why conditional rendering matters in Vue - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Conditional Rendering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What will this Vue component display initially?

Consider this Vue component using conditional rendering with @if. What text will appear when the component first renders?

Vue
<template>
  <div>
    <p @if="showMessage">Hello, Vue learner!</p>
    <button @click="showMessage = !showMessage">Toggle Message</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const showMessage = ref(false)
</script>
AAn error occurs because @if is not valid syntax.
BThe text 'Hello, Vue learner!' is visible.
CNo text is visible initially.
DThe button text 'Toggle Message' is hidden.
Attempts:
2 left
💡 Hint

Think about the initial value of showMessage and how @if controls rendering.

state_output
intermediate
2:00remaining
What is the value of count after clicking the button twice?

This Vue component toggles a message and increments a count only when the message is shown. What is the value of count after clicking the button two times?

Vue
<template>
  <div>
    <p @if="show">Message shown</p>
    <button @click="toggle">Toggle</button>
    <p>Count: {{ count }}</p>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const show = ref(false)
const count = ref(0)

function toggle() {
  show.value = !show.value
  if (show.value) {
    count.value++
  }
}
</script>
A3
B0
C2
D1
Attempts:
2 left
💡 Hint

Count increments only when the message becomes visible.

📝 Syntax
advanced
2:00remaining
Which option correctly uses Vue's conditional rendering syntax?

Vue 3.4+ introduced new control flow directives. Which of these options correctly shows a paragraph only if isVisible is true?

A<p v-show="isVisible">Visible</p>
B<p @if="isVisible">Visible</p>
C<p v-if="isVisible">Visible</p>
D<p :if="isVisible">Visible</p>
Attempts:
2 left
💡 Hint

Vue 3.4+ uses @if as a new control flow directive replacing v-if.

🔧 Debug
advanced
2:00remaining
Why does this Vue component fail to toggle the message?

Look at this Vue component. Clicking the button does not show or hide the message. What is the cause?

Vue
<template>
  <div>
    <p @if="showMessage">Hello!</p>
    <button @click="toggleMessage">Toggle</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const showMessage = ref(false)

function toggleMessage() {
  showMessage = !showMessage
}
</script>
AThe toggleMessage function incorrectly reassigns the ref variable instead of its value.
BThe @if directive is not supported in Vue 3.4+.
CThe button click event is missing parentheses.
DThe showMessage variable is not declared.
Attempts:
2 left
💡 Hint

Remember how to update a ref value in Vue.

🧠 Conceptual
expert
2:00remaining
Why is conditional rendering important for performance in Vue?

Which statement best explains why conditional rendering matters in Vue applications?

AIt prevents unnecessary elements from being created and updated, saving browser resources and improving speed.
BIt automatically caches all components to avoid re-rendering.
CIt forces Vue to render all elements at once for consistency.
DIt disables reactivity to improve performance.
Attempts:
2 left
💡 Hint

Think about how rendering fewer elements affects browser work.