DEV Community

Cover image for A guide to Vue Lifecycle hooks.
Kolly
Kolly

Posted on

A guide to Vue Lifecycle hooks.

What you will learn

Understanding all the vue js hooks, vuex ( a state management tool ), and state options, will give you the flexibility you need to build a functional software product. This article will introduce you to vue js hooks , it will also give you the basic understanding on how and when to use these hooks. However, if you are willing to know more about the related topics stated above, here is a link to guide you on that.

Prerequisite

A basic knowledge of vue js is enough for you to quickly grasp all the concept i will be discussing in this article. Also, having a solid foundation on other front-end frameworks will make it easier for you to understand faster. However, it's not a requirement.

Vue js lifecycle hooks

  1. beforeCreate
  2. created
  3. beforeMount
  4. mounted
  5. beforeUpdate
  6. updated
  7. beforeUnmount
  8. unmounted

Let's take a closer look at how and when to use these hooks.

beforeCreate

It's called once, when the vue instance is being initialized, what do i mean about "initialized vue instance". Well, a vue instance is initialized so that the data, watcher, computed, methods can processed. you can also call this data related options(state options).

beforeCreate(){
  console.log('instanced initialized')
}
Enter fullscreen mode Exit fullscreen mode

created

Created is called after all the state options has been processed. However, the instance has not been mounted to the DOM (document object model). You cannot access the DOM at this stage, because the component have not been mounted. You can only fetch data from back-end, and also manipulate the reactive data.

created(){
  console.log("is Processed state options'")
}
Enter fullscreen mode Exit fullscreen mode

beforeMount

This is the stage where the created hook has been completed , the reactive state has been processed , and ready to be mounted on the DOM . What happen before it's being mounted ? think about it !..before it's being mounted.

beforeMount(){
   console.log("before mount")   
}
Enter fullscreen mode Exit fullscreen mode

Mounted

Mounted is called after the component DOM has been created and added to the parent component. You can access the reactive component, manipulate the DOM elements.

mounted(){
    console.log("mounted")
}
Enter fullscreen mode Exit fullscreen mode

beforeUpdate

This hook can be used to modify the DOM, before it's updated. it's invoked immediately after a part of the component being rendered changed, due to re-evaluation in the data options.

beforeUpdated(){
    console.log("before component update")
}
Enter fullscreen mode Exit fullscreen mode

updated

This hook is called in your application when there is a change in the reactive data , which has caused a DOM update of the component. However, a lot of people still confuse this to watcher, which listen to change in reactive data, while updated hook listen to change in virtual DOM.

updated(){
    console.log("updated");
}
Enter fullscreen mode Exit fullscreen mode

beforeUnmount

This hook is called right before a component is unmounted, while the component instance is still active and working efficiently.

beforeUnmount(){
   console.log("before unmount")
}
Enter fullscreen mode Exit fullscreen mode

unmounted

Vue instance have been unmounted , if the component instance , DOM , reactive data , watcher has been stopped. you can use this if you have to inform a server that your component has been unmounted or send analysis.

unmounted(){
   console.log("component unmounted")
}
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this article , you were introduced to vue js hooks and its use case. you can apply this knowledge by implementing these hooks in your application. Don't forget to like share and comment, happy coding!!!

Stay tune for my next blog series, Rudiments.

Top comments (0)