Vue3 Fundamentals ยท Built-in Components

Skip work with v-memo

Skipping re-renders with v-memo

Skip work with v-memo

A long list re-renders every row whenever any value changes, even the rows whose content stayed the same. v-memo lets you tell Vue which rows it can skip.

You give v-memo an array of dependencies. Vue renders the element once, then reuses that result until one of the dependencies changes:

<li v-for="item in items" :key="item.id" v-memo="[item.id, item.score]">
  {{ item.label }}: {{ item.score }}
</li>

The row re-renders only when item.id or item.score changes. List every value the row reads that can change; a value you leave out will not update the row, so the text goes stale. That is the trade: less work, more care.

An empty array, v-memo="[]", means the row never re-renders after the first pass. v-memo is used inside v-for, where the savings are largest.

The Test changes item.score and checks the row shows the new value. The starter lists no dependencies, so the row is frozen and the Test fails.

Challenge

The row is frozen and never updates. Give v-memo the value it shows: export an App component from App.vue whose <li> uses v-memo="[item.score]".

The Test changes item.score and expects the row to show the new value. The starter uses an empty dependency list, so the row never re-renders and the Test fails.

App.vue
<script setup>
import { ref } from 'vue';

const items = ref([{ id: 1, label: 'Alpha', score: 0 }]);

function addScore() {
  items.value[0].score += 1;
}
</script>

<template>
  <ul>
    <li v-for="item in items" :key="item.id" v-memo="[]" data-test="row">
      {{ item.label }}: {{ item.score }}
    </li>
  </ul>
  <button data-test="bump" @click="addScore">Add score</button>
</template>

No test run yet

The Test is hidden. Run it to check the Challenge โ€” you only see the result, never the test source.