Back to the blog

Vue component lifecycle

vuehooksfundamentals

The lifecycle of a Vue component is made up of the different stages a component goes through, from its creation to its destruction. Each of these stages gives you an opportunity to perform a specific action, such as loading data or modifying the DOM.

Order of the lifecycle stages

  1. beforeCreate: Happens before anything else. The component hasn't been initialized yet and doesn't have access to this.
  2. created: Happens right after the component is initialized. The component hasn't been mounted yet and doesn't have access to the DOM. This is the ideal moment to load data or initialize state variables.
  3. beforeMount: Happens before the component is mounted to the DOM.
  4. mounted: Happens when the component is mounted to the DOM. This is the ideal moment to access the DOM or initialize external plugins.
  5. beforeUpdate: Happens before a component update is rendered.
  6. updated: Happens after a component update is rendered.
  7. beforeDestroy: Happens before the component is destroyed.
  8. destroyed: Happens when the component is destroyed.

It's important to note that, depending on your application's context, not all of these stages will be necessary or used. It's also worth knowing that this order can vary depending on the circumstances, for example, when navigation leads to a route that reuses the same component, beforeMount and mounted are not called again.