#1 Data Analytics Program in India
₹2,499₹1,499Enroll Now
4 min read
Question 6 of 37easy

What are Vue Lifecycle Hooks?

Understanding component lifecycle in Vue.

What You'll Learn

  • Lifecycle stages
  • Common hooks
  • Composition API hooks

Lifecycle Diagram

Creation → Mounting → Updating → Unmounting

Composition API Hooks

code.jsJavaScript
import {
  onBeforeMount,
  onMounted,
  onBeforeUpdate,
  onUpdated,
  onBeforeUnmount,
  onUnmounted
} from 'vue'

// Before DOM is mounted
onBeforeMount(() => {
  console.log('Before mount')
})

// After DOM is mounted
onMounted(() => {
  console.log('Mounted - DOM available')
})

// Before reactive update
onBeforeUpdate(() => {
  console.log('Before update')
})

// After reactive update
onUpdated(() => {
  console.log('Updated')
})

// Before unmount
onBeforeUnmount(() => {
  console.log('Cleanup here')
})

// After unmount
onUnmounted(() => {
  console.log('Unmounted')
})

Common Use Cases

HookUse Case
onMountedFetch data, DOM access
onUpdatedReact to DOM changes
onBeforeUnmountCleanup timers, listeners