0
0
Vueframework~20 mins

v-once for static content in Vue - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Vue v-once Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does v-once do in this Vue component?
Consider this Vue template snippet:
<template>
  <div>
    <h1 v-once>Hello, {{ name }}!</h1>
    <button @click="changeName">Change Name</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const name = ref('Alice')
function changeName() {
  name.value = 'Bob'
}
</script>

What will be displayed inside the <h1> after clicking the button?
Vue
<template>
  <div>
    <h1 v-once>Hello, {{ name }}!</h1>
    <button @click="changeName">Change Name</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const name = ref('Alice')
function changeName() {
  name.value = 'Bob'
}
</script>
AIt stays 'Hello, Alice!' even after clicking the button.
BIt causes a runtime error because v-once cannot be used with variables.
CIt shows 'Hello, Bob!' only after refreshing the page.
DIt updates to 'Hello, Bob!' immediately after clicking the button.
Attempts:
2 left
💡 Hint
Think about what v-once does to the element's rendering and updates.
state_output
intermediate
2:00remaining
How many times is the computedValue updated with v-once?
Given this Vue component:
<template>
  <p v-once>Computed: {{ computedValue }}</p>
  <button @click="increment">Increment</button>
</template>

<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const computedValue = computed(() => count.value * 2)
function increment() {
  count.value++
}
</script>

How many times will the computedValue be updated and reflected in the UI after clicking the button 3 times?
Vue
<template>
  <p v-once>Computed: {{ computedValue }}</p>
  <button @click="increment">Increment</button>
</template>

<script setup>
import { ref, computed } from 'vue'
const count = ref(0)
const computedValue = computed(() => count.value * 2)
function increment() {
  count.value++
}
</script>
AThe computed value updates but the UI does not reflect changes until refresh.
BThe computed value updates and shows 2, 4, 6 after each click.
CThe computed value causes an error because v-once blocks computed properties.
DThe computed value is shown only once with initial value 0 and never updates.
Attempts:
2 left
💡 Hint
Remember what v-once does to the element's reactivity.
📝 Syntax
advanced
2:00remaining
Which option correctly uses v-once to optimize static content?
You want to render a static list of items that never change. Which Vue template snippet correctly uses v-once to optimize rendering?
A
&lt;ul&gt;
  &lt;li v-for="item in items" :key="item.id" v-once&gt;{{ item.name }}&lt;/li&gt;
&lt;/ul&gt;
B
&lt;ul v-once&gt;
  &lt;li v-for="item in items" :key="item.id"&gt;{{ item.name }}&lt;/li&gt;
&lt;/ul&gt;
C
&lt;ul&gt;
  &lt;li v-for="item in items" :key="item.id"&gt;{{ item.name }}&lt;/li&gt;
&lt;/ul&gt; &lt;!-- v-once on comment --&gt;
D
&lt;ul v-once&gt;
  &lt;li v-for="item in items"&gt;{{ item.name }}&lt;/li&gt;
&lt;/ul&gt;
Attempts:
2 left
💡 Hint
Think about how v-once works with v-for and keys.
🔧 Debug
advanced
2:00remaining
Why does this Vue component not update the static text inside v-once after state change?
Look at this Vue component:
<template>
  <div>
    <p v-once>Static message: {{ message }}</p>
    <button @click="updateMessage">Update</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function updateMessage() {
  message.value = 'Goodbye'
}
</script>

Why does the text inside <p> not change after clicking the button?
Vue
<template>
  <div>
    <p v-once>Static message: {{ message }}</p>
    <button @click="updateMessage">Update</button>
  </div>
</template>

<script setup>
import { ref } from 'vue'
const message = ref('Hello')
function updateMessage() {
  message.value = 'Goodbye'
}
</script>
ABecause the message variable is not reactive and does not trigger updates.
BBecause v-once renders the element only once and disables future updates.
CBecause the button click handler is not correctly bound to the updateMessage function.
DBecause Vue requires a key attribute on the <p> element to update it.
Attempts:
2 left
💡 Hint
Think about what v-once does to the element's rendering lifecycle.
🧠 Conceptual
expert
2:00remaining
What is the main benefit of using v-once in Vue components?
Choose the best explanation for why and when to use v-once in Vue templates.
AIt enables two-way data binding for static content to keep it reactive.
BIt automatically caches API responses to reduce network requests.
CIt improves performance by rendering static content only once and skipping future updates.
DIt forces Vue to re-render the element every time the state changes.
Attempts:
2 left
💡 Hint
Think about what happens when you have content that never changes.