0
0
Vueframework~10 mins

Lifecycle hooks in Composition API in Vue - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Lifecycle hooks in Composition API
Setup function runs
onBeforeMount hook
Component mounts to DOM
onMounted hook
Reactive data changes?
Component updates
onBeforeUpdate hook
DOM updates
onUpdated hook
Component unmounts
onBeforeUnmount hook
onUnmounted hook
Cleanup done
This flow shows how Vue Composition API lifecycle hooks run in order during component setup, mounting, updating, and unmounting.
Execution Sample
Vue
import { ref, onMounted, onBeforeUnmount } from 'vue';

export default {
  setup() {
    const count = ref(0);
    onMounted(() => console.log('Mounted'));
    onBeforeUnmount(() => console.log('Before Unmount'));
    return { count };
  }
}
This code sets up a reactive count and logs messages when the component mounts and before it unmounts.
Execution Table
StepHook TriggeredActionConsole OutputComponent State
1setup()Initialize count=0count=0
2onMountedRun mounted callbackMountedcount=0
3Reactive changecount incremented to 1count=1
4onBeforeUpdateBefore DOM updatescount=1
5DOM updatesRender new countcount=1
6onUpdatedAfter DOM updatescount=1
7onBeforeUnmountRun before unmount callbackBefore Unmountcount=1
8onUnmountedCleanup after unmountcount=1
💡 Component unmounted, lifecycle ends
Variable Tracker
VariableStartAfter Step 3Final
count011
Key Moments - 3 Insights
Why does onMounted run after setup but before reactive changes?
Because setup initializes state first, then onMounted runs after the component is attached to the DOM but before reactive changes trigger updates, as shown in steps 1 and 2.
What happens if reactive data changes after mounting?
The onBeforeUpdate and onUpdated hooks run around the DOM update to reflect the new state, as seen in steps 3 to 6.
When do onBeforeUnmount and onUnmounted run?
They run just before and after the component is removed from the DOM, allowing cleanup, as shown in steps 7 and 8.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of count after step 3?
A1
Bundefined
C0
D2
💡 Hint
Check the 'Component State' column at step 3 in the execution_table.
At which step does the component first log 'Mounted'?
AStep 1
BStep 2
CStep 5
DStep 7
💡 Hint
Look at the 'Console Output' column to find when 'Mounted' appears.
If the count never changes, which hooks related to updates will NOT run?
AonMounted and onBeforeUnmount
BonBeforeUnmount and onUnmounted
ConBeforeUpdate and onUpdated
Dsetup and onMounted
💡 Hint
Refer to steps 4 and 6 where update hooks run after reactive changes.
Concept Snapshot
Vue Composition API lifecycle hooks:
- setup() runs first to initialize state
- onMounted runs after component mounts
- onBeforeUpdate and onUpdated run around reactive updates
- onBeforeUnmount and onUnmounted run during unmount
Use these hooks to run code at key component moments.
Full Transcript
This visual trace shows how Vue's Composition API lifecycle hooks work step-by-step. First, the setup function runs to create reactive data. Then onMounted runs after the component is attached to the page. When reactive data changes, onBeforeUpdate runs before the DOM updates, and onUpdated runs after. Finally, when the component is removed, onBeforeUnmount and onUnmounted run to allow cleanup. The execution table tracks these steps with state and console outputs, helping beginners see exactly when each hook triggers during the component's life.