The onUpdated Hook
The onUpdated hook runs a function after Vue redraws the component. onUpdated is the hook for work that must follow a change on the page.
Register the hook
The hook marks updated as true after each redraw.
onUpdated(() => {
updated.value = true
})The starting values
The first render shows Count 0 and Updated No.
<p>Count: {{ count }}</p>Example
<script setup>
import { ref, onUpdated } from 'vue'
const count = ref(0)
const updated = ref(false)
onUpdated(() => {
updated.value = true
})
function add() {
count.value++
}
</script>
<template>
<div>
<p>Count: {{ count }}</p>
<p>Updated: {{ updated ? 'Yes' : 'No' }}</p>
<button @click="add">Add</button>
</div>
</template>
Key points
- onUpdated takes one function, and Vue calls it after each redraw.
- The hook runs when data changes and Vue draws the new values.
- It does not run after the first draw, because that draw is mounting.
- The hook does not run during a static render, so the first output shows the starting values.
- The hook must not change the data that caused the redraw, or the redraws never stop.
In this example, count is a ref with the value 0, and updated is a ref with the value false. onUpdated registers a function that sets updated to true. add raises count by one. The hook does not run in a static render, so the stated output shows Count 0 and Updated No. After a click in a live app, Vue redraws and the hook runs. The page then shows Count 1 and Updated Yes. A verified variant with those values shows the post-update render.
Note
Import onUpdated from vue before calling it.
Try it Yourself
Edit the code, then press Run.
Running is simulated
Exercise
When does onUpdated run?