These functions let you run code right before or after a component updates. This helps you react to changes smoothly.
onUpdated and onBeforeUpdate in Vue
import { onBeforeUpdate, onUpdated } from 'vue'; export default { setup() { onBeforeUpdate(() => { // code to run before update }); onUpdated(() => { // code to run after update }); } }
Use these inside the setup() function of a Vue component.
onBeforeUpdate runs before the DOM updates, onUpdated runs after.
onBeforeUpdate(() => {
console.log('Component will update soon');
});onUpdated(() => {
console.log('Component has updated');
});onBeforeUpdate(() => {
// Reset a timer or cancel a request before update
});
onUpdated(() => {
// Start a timer or fetch data after update
});This component shows a number and a button. When you click the button, the number increases. The console logs the count before and after the update.
<template>
<div>
<p>Count: {{ count }}</p>
<button @click="increment">Increase</button>
</div>
</template>
<script setup>
import { ref, onBeforeUpdate, onUpdated } from 'vue';
const count = ref(0);
function increment() {
count.value++;
}
onBeforeUpdate(() => {
console.log('Before update: count is', count.value);
});
onUpdated(() => {
console.log('After update: count is', count.value);
});
</script>These hooks only work inside the setup() function or <script setup>.
They run every time the component updates, so avoid heavy work inside them.
Use console or DevTools to see when these hooks run during development.
onBeforeUpdate runs just before the component updates the screen.
onUpdated runs right after the screen updates.
Use them to react to changes smoothly and keep your UI in sync.
Practice
onBeforeUpdate and onUpdated in Vue?Solution
Step 1: Understand lifecycle timing
onBeforeUpdatetriggers just before Vue updates the DOM to reflect data changes.Step 2: Identify post-update hook
onUpdatedtriggers right after the DOM has been updated.Final Answer:
onBeforeUpdateruns before the screen updates;onUpdatedruns after. -> Option DQuick Check:
Before update = onBeforeUpdate, After update = onUpdated [OK]
- Confusing the order of hooks
- Thinking both run at the same time
- Assuming onUpdated runs only once
onBeforeUpdate in a Vue 3 component?Solution
Step 1: Check import syntax
Vue exportsonBeforeUpdateas a named export, so use curly braces in import.Step 2: Use hook as a function
CallonBeforeUpdatewith a callback function to run code before update.Final Answer:
import { onBeforeUpdate } from 'vue'; onBeforeUpdate(() => { console.log('Before update'); }); -> Option AQuick Check:
Named import + callback function = import { onBeforeUpdate } from 'vue'; onBeforeUpdate(() => { console.log('Before update'); }); [OK]
- Importing without curly braces
- Assigning hook to a variable instead of calling it
- Trying to call a method on the hook
count changes from 0 to 1?import { ref, onBeforeUpdate, onUpdated } from 'vue';
export default {
setup() {
const count = ref(0);
onBeforeUpdate(() => console.log('Before update:', count.value));
onUpdated(() => console.log('After update:', count.value));
return { count };
}
}Solution
Step 1: Understand timing of hooks
onBeforeUpdateruns after reactive data updated (to 1) but before DOM updates, socount.valueis 1.Step 2: Check
onUpdatedtimingonUpdatedruns after DOM updates,count.valueis 1.Final Answer:
Before update: 1 After update: 1 -> Option BQuick Check:
Both hooks log new value [OK]
- Thinking onBeforeUpdate logs old value
- Mixing order of logs
- Thinking onBeforeUpdate runs after value changes
onBeforeUpdate and onUpdated? import { ref, onBeforeUpdate, onUpdated } from 'vue';
export default {
setup() {
const count = ref(0);
onBeforeUpdate = () => console.log('Before update');
onUpdated(() => console.log('After update'));
count.value++;
return { count };
}
}Solution
Step 1: Check hook usage
The code assigns a function toonBeforeUpdateinstead of calling it with a callback.Step 2: Confirm other parts
Imports are correct,count.value++is valid, andcountis returned properly.Final Answer:
Assigning a function to onBeforeUpdate instead of calling it. -> Option AQuick Check:
Hooks must be called, not reassigned [OK]
- Assigning hooks instead of calling
- Forgetting to import hooks
- Misusing ref value increment
score every time it updates in a Vue 3 component. Which approach correctly uses onBeforeUpdate and onUpdated to achieve this?import { ref, onBeforeUpdate, onUpdated } from 'vue';
export default {
setup() {
const score = ref(0);
let oldScore = 0;
onBeforeUpdate(() => {
oldScore = score.value;
});
onUpdated(() => {
console.log(`Score changed from ${oldScore} to ${score.value}`);
});
return { score };
}
}Solution
Step 1: Understand hook order
onBeforeUpdateruns after data updated, so setsoldScoreto new value.Step 2: Confirm logging after update
onUpdatedlogs new value for both old and current.Final Answer:
This code logs the same value twice because oldScore is set to the new value in onBeforeUpdate. -> Option CQuick Check:
onBeforeUpdate cannot capture old ref value; use watch() instead [OK]
- Thinking onBeforeUpdate sees old data value
- Expecting old/new to differ with this pattern
- Fearing infinite loops from hook logic
Ready to prove it?
Pass the test and this pattern moves unknown
