0
0
Vueframework~10 mins

onUpdated and onBeforeUpdate in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - onUpdated and onBeforeUpdate
Component Render Start
onBeforeUpdate Hook Runs
DOM Updates
onUpdated Hook Runs
Render Cycle Ends or Repeats
This flow shows how Vue calls onBeforeUpdate before the DOM changes, then updates the DOM, and finally calls onUpdated after the DOM is updated.
Execution Sample
Vue
import { ref, onBeforeUpdate, onUpdated } from 'vue';

export default {
  setup() {
    const count = ref(0);
    onBeforeUpdate(() => console.log('Before update:', count.value));
    onUpdated(() => console.log('Updated:', count.value));
    function increment() { count.value++; }
    return { count, increment };
  }
}
This Vue component logs messages before and after the DOM updates when the count changes.
Execution Table
StepTriggercount.valueHook CalledConsole OutputDOM State
1Initial render0Nonecount displayed as 0
2increment() called1onBeforeUpdateBefore update: 1DOM still shows 0
3DOM updates1Nonecount displayed as 1
4After DOM update1onUpdatedUpdated: 1count displayed as 1
5increment() called again2onBeforeUpdateBefore update: 2DOM still shows 1
6DOM updates2Nonecount displayed as 2
7After DOM update2onUpdatedUpdated: 2count displayed as 2
8No more changes2NoneDOM stable with count 2
💡 No more state changes, so no further hooks or DOM updates occur.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 5After Step 6Final
count.value011222
Key Moments - 3 Insights
Why does onBeforeUpdate log the new count value before the DOM changes?
Because onBeforeUpdate runs after Vue updates the reactive data but before the DOM, so count.value holds the new value while the DOM still shows the old value (see Step 2 and Step 5 in execution_table).
Why does onUpdated log the new count value after the DOM updates?
Because onUpdated runs after Vue has updated the DOM and reactive data, so count.value reflects the new value (see Step 4 and Step 7 in execution_table).
Does onBeforeUpdate run on the initial render?
No, onBeforeUpdate only runs before updates, not on the first render (see Step 1 where no hook is called).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is count.value when onBeforeUpdate logs at Step 2?
A1
B0
Cundefined
D2
💡 Hint
Check the 'count.value' and 'Console Output' columns at Step 2 in execution_table.
At which step does the DOM first show the updated count value?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'DOM State' column to see when the count changes visually.
If increment() was not called again after Step 7, what would happen next?
AonBeforeUpdate would run again
BonUpdated would run again
CNo hooks would run and DOM stays the same
Dcount.value resets to 0
💡 Hint
Refer to Step 8 in execution_table and the exit_note.
Concept Snapshot
Vue calls onBeforeUpdate before the DOM updates to let you react to upcoming changes.
onUpdated runs after the DOM updates to confirm changes happened.
Neither hook runs on the initial render.
Use these hooks to track or react to DOM changes safely.
They help manage side effects around reactive data updates.
Full Transcript
This visual trace shows how Vue's onBeforeUpdate and onUpdated hooks work during component updates. Initially, the component renders with count 0 and no hooks run. When increment() is called, it first updates the reactive count.value. Then onBeforeUpdate runs, logging the new count value before the DOM changes. Then Vue updates the DOM. After that, onUpdated runs, logging the new count value. This cycle repeats with each increment call. The key point is that onBeforeUpdate sees the new reactive state before the DOM changes, and onUpdated sees the new state after DOM changes. Neither hook runs on the first render. This helps developers run code safely around DOM updates. The variable tracker shows count.value changing from 0 to 2 over steps. The execution table details each step's state, hook calls, and console output. The quiz questions check understanding of when hooks run and what values they see.