v-memo helps Vue remember parts of your template so it doesn't redo work when it doesn't need to. This makes your app faster by skipping repeated work.
v-memo for conditional memoization in Vue
<template> <div v-memo="[condition1, condition2]"> <!-- content that updates only if conditions change --> </div> </template>
The v-memo directive takes an array of values to watch.
Vue skips re-rendering the element if these values do not change.
count changes.<template>
<p v-memo="[count]">Count is {{ count }}</p>
</template>item.value changes.<template>
<ul>
<li v-for="item in items" :key="item.id" v-memo="[item.value]">
{{ item.value }}
</li>
</ul>
</template>user.name or user.age changes.<template>
<div v-memo="[user.name, user.age]">
<p>Name: {{ user.name }}</p>
<p>Age: {{ user.age }}</p>
</div>
</template>Here, the paragraph showing count uses v-memo with [count]. It only re-renders when count changes. The name paragraph updates normally.
Clicking 'Increment Count' updates the count and re-renders the count paragraph only. Clicking 'Change Name' updates the name but does not re-render the count paragraph.
<template>
<div>
<button @click="incrementCount">Increment Count</button>
<button @click="changeName">Change Name</button>
<p v-memo="[count]">Count: {{ count }}</p>
<p>Name: {{ name }}</p>
</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
const name = ref('Alice')
function incrementCount() {
count.value++
}
function changeName() {
name.value = name.value === 'Alice' ? 'Bob' : 'Alice'
}
</script>Time complexity: Using v-memo reduces unnecessary re-renders, improving performance especially in large or complex templates.
Space complexity: Minimal extra memory is used to track the memoized values.
A common mistake is not including all relevant reactive values in the v-memo array, causing stale or incorrect UI.
Use v-memo when you want fine control over updates. For simple cases, Vue's normal reactivity is enough.
v-memo helps Vue skip re-rendering parts of the template when certain values don't change.
It takes an array of values to watch and only updates if these change.
This improves performance by avoiding unnecessary work.
Practice
v-memo in Vue?Solution
Step 1: Understand what
v-memodoesv-memowatches specific values and skips re-rendering if they remain unchanged.Step 2: Compare options with this behavior
Only To skip re-rendering parts of the template when watched values don't change describes skipping re-rendering based on watched values, matchingv-memo's purpose.Final Answer:
To skip re-rendering parts of the template when watched values don't change -> Option AQuick Check:
v-memo skips re-rendering [OK]
- Confusing v-memo with event handling
- Thinking v-memo creates reactive data
- Assuming v-memo forces re-rendering
v-memo watching variables count and user.name?Solution
Step 1: Recall the syntax for
v-memov-memoexpects an array of values to watch, so the correct syntax uses square brackets.Step 2: Check each option's syntax
<div v-memo="[count, user.name]">...uses an array with square brackets, which is correct. Options A, B, and C use incorrect formats (logical AND, comma-separated without brackets, object).Final Answer:
<div v-memo="[count, user.name]">... -> Option AQuick Check:
v-memo needs array syntax [OK]
- Omitting brackets around watched values
- Using object braces instead of array brackets
- Using logical operators instead of array
<template>
<div v-memo="[count]">Count is {{ count }}</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
setTimeout(() => { count.value = 0 }, 1000)
</script>What will happen after 1 second?
Solution
Step 1: Understand the watched value and update
Thev-memowatchescount. After 1 second,count.valueis set to 0, which is the same as its initial value.Step 2: Determine if re-render occurs
Since the watched value did not change,v-memoskips re-rendering the div.Final Answer:
The div will not re-render because count did not change -> Option DQuick Check:
v-memo skips if values unchanged [OK]
- Assuming re-render happens even if value unchanged
- Confusing ref value assignment with new value
- Thinking v-memo causes errors on no change
v-memo:<template>
<div v-memo="count">Count: {{ count }}</div>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
</script>Solution
Step 1: Check
v-memosyntax requirementsv-memorequires an array of values to watch, so the value must be inside square brackets.Step 2: Analyze the given code
The code usesv-memo="count"without brackets, which is incorrect syntax.Final Answer:
v-memo expects an array, but count is not in brackets -> Option CQuick Check:
v-memo needs array syntax [OK]
- Using single variable without brackets
- Confusing refs with computed properties
- Thinking v-memo requires v-if
status and value. You only want to re-render an item if its status changes, ignoring value. How should you use v-memo inside the v-for loop?Solution
Step 1: Identify which value controls re-rendering
You want to re-render only whenitem.statuschanges, ignoringitem.value.Step 2: Use
Settingv-memowith the correct watched valuev-memo="[item.status]"watches only the status, so changes in value won't trigger re-render.Final Answer:
<li v-for="item in items" :key="item.id" v-memo="[item.status]">{{ item.value }}</li> -> Option BQuick Check:
Watch only status for re-render [OK]
- Watching value instead of status
- Watching both status and value unnecessarily
- Not using v-memo and causing extra renders
Ready to prove it?
Pass the test and this pattern moves unknown
