v-memo affect component rendering?v-memo with a dependency array. What happens when a reactive property outside the dependency array changes?<template> <div v-memo="[count]">Count is: {{ count }}</div> <button @click="increment">Increment</button> <button @click="changeName">Change Name</button> </template> <script setup> import { ref } from 'vue' const count = ref(0) const name = ref('Vue') function increment() { count.value++ } function changeName() { name.value = name.value === 'Vue' ? 'JS' : 'Vue' } </script>
v-memo watches to decide re-rendering.v-memo tells Vue to re-render the element only if the values in its dependency array change. Since only count is in the array, changes to name do not trigger re-render.
v-memo syntax for multiple dependenciesv-memo with two reactive dependencies foo and bar?v-memo expects an array of dependencies.The correct syntax uses an array to list dependencies: [foo, bar]. Other options are invalid JavaScript expressions or do not represent arrays.
v-memo not prevent re-rendering?count does not change?
<template>
<div v-memo="[count]">Count: {{ count }}</div>
<button @click="increment">Increment</button>
</template>
<script setup>
import { ref } from 'vue'
const count = ref(0)
function increment() {
count.value++
}
</script>
Assume the button is clicked but count is not incremented.v-memo is used.v-memo prevents re-render if dependencies do not change. If the element re-renders without count changing, another reactive source or parent re-render caused it.
v-memo?<template>
<div v-memo="[show]">Visible: {{ show }}</div>
<button @click="toggleShow">Toggle</button>
</template>
<script setup>
import { ref } from 'vue'
const show = ref(true)
function toggleShow() {
show.value = !show.value
}
</script>
What will the div display after clicking the button twice?show value, which is a dependency of v-memo.Since show is in the v-memo dependency array, toggling it causes the div to re-render and update its displayed value accordingly.
v-memo for conditional memoization in Vue?v-memo in Vue components?v-memo helps Vue skip rendering parts of the template when dependencies are unchanged, which can reduce unnecessary work and improve app speed.
