Skip to main content

Lifecycle hooks

Vue 3 has two sets of lifecycle hooks:

  1. Hooks for the Composition API
  2. Hooks for the Options API

Lifecycle hooks (Composition API)

Used inside <script setup> or a regular setup().

Component mounting

HookWhen it is called
onBeforeMountbefore the component is inserted into the DOM
onMountedafter the component is inserted into the DOM

Component updating

HookWhen it is called
onBeforeUpdatebefore the DOM updates
onUpdatedafter the DOM updates

Component unmounting

HookWhen it is called
onBeforeUnmountbefore the component is removed
onUnmountedafter it is removed from the DOM

Error handling

HookWhen it is called
onErrorCapturedwhen an error is caught in a child component

Transitions (animations)

HookWhen it is called
onRenderTrackedtracks reactivity dependencies
onRenderTriggeredcalled when a reactive update triggers

(rarely used, but they exist)

keep-alive components

HookWhen it is called
onActivatedwhen the component is "activated" (returns from the cache)
onDeactivatedwhen the component is "deactivated" (goes into the cache)

Hooks (Options API)

If the old format is used (export default {}).

Mounting:

  • beforeCreate
  • created
  • beforeMount
  • mounted

Updating:

  • beforeUpdate
  • updated

Unmounting:

  • beforeUnmount
  • unmounted

Keep-alive:

  • activated
  • deactivated

Errors:

  • errorCaptured

Summary (cheat sheet)

Composition API (main):

  • onBeforeMount
  • onMounted
  • onBeforeUpdate
  • onUpdated
  • onBeforeUnmount
  • onUnmounted
  • onErrorCaptured
  • onActivated / onDeactivated
  • onRenderTracked / onRenderTriggered

Options API (old format):

  • beforeCreate / created
  • beforeMount / mounted
  • beforeUpdate / updated
  • beforeUnmount / unmounted
  • activated / deactivated
  • errorCaptured

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.