Vue js lifecycle hooks for dummies
Tags: vue, frontend, javascript
Published: 2021-06-21 15:29:02

## What are lifecycle hooks? 
Every component in VueJS has a lifecycle which is being managed by Vue itself when it creates the component, mounts the component to the DOM, updates the component and destroy the components. 

![Vue js lifcycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rzanx1fsmphhu8arl3ka.jpg)
Just like Humans have a lifecycle (birth , growing up , death etc)  We can tap into key moments in that lifecycle by implementing one or more lifecycle hooks giving us the opportunity to add our own code at specific stages of a component lifetime.

**for the sake of this article, we will consider a Vue component as a package that will contain HTML / CSS / JS** 
![Vue component lifcycle](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lbkzk1l4x8b547ii3y45.jpg)


## - beforeCreate()

![beforeCreate Vuejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mi90oxy6ipnh6v7qtsht.png)

Our package is now being filled with events/watchers setups and data.

**Explanation :** Inside this hook, data is still not reactive and events that occur during the component’s lifecycle have not been set up yet.
we can not use any Data/ Methods from the Component, however you can access [props] (https://v3.vuejs.org/guide/component-props.html).

## - Created() 
![created vuejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ih9i96i0gxoswp1yr0s.png)
Our package is finally filled with reactive data , computed properties, methods, watch/event callbacks.

**Explanation :**  Data is now reactive and events have been set up You cannot do any DOM manipulations because DOM has not been mounted yet. created() is called earlier in order to trigger actions like **data fetching** from API backend. 
> Created will not wait for all of the async operations to complete before moving on to the next stage when making API call.

## - BeforeMount() 
![beforeMount vuejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bal84228rguyafa1mlfr.png)
the package is on it's way to our depot "The Dom" 
**Explanation :**   the render function is being called for the first time , our template has been compiled and our virtual DOM updated by Vue. 
> This hook is not called during server-side rendering.


## - Mounted() 
![mounted vuejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/de99sjy1dqsq94njj10p.png)
the package is now stored in the depot "THE DOM" and have access inside the dom.

**Explanation :** with this hook you can  access or modify the DOM of your component immediately before or after the initial render.
The Vue docs recommend using the mounted() hook over the created() hook for data fetching. 
**Vue does not block rendering until the mounted function is done running, so mounted()  runs concurrently with axios .** 


> This hook is not called during server-side rendering.


## - beforeUpdate() 
![beforeUpdate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/a9y3yxyg6gxkxppbzerj.jpg)
the package content is being changed, so it's back to our factory and not accessible to "the DOM" depot anymore. 

**Explanation :** this hook is called when data changes, but the DOM hasn't changed yet. 
The Vue Docs recommends using this hook to remove manually added event listeners.
> This hook is not called during server-side rendering, because only the initial render is performed server-side.


## - Updated() 
![Updated](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dqlgbddly5puaogpqm7m.jpg)
the package content is back to "the DOM"  depot after the changes.

**Explanation :** Updated() is called when there is some data changes that causes the DOM to be re-rendered and patched. This hook is mostly for changes related to the DOM. It is not recommended updating the state with this hook. It's better to  use [computed property](https://v3.vuejs.org/api/options-data.html#computed) or [Watchers](https://v3.vuejs.org/api/options-data.html#watch) to react to state changes.
> This hook is not called during server-side rendering, because only the initial render is performed server-side.


## - unmounted() 
![unmounted vuejs](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4pkej2yazo31q1re5u50.png)
we no longer need the package and it's going straight to the trash.

**Explanation :** all directives of the component instance have been destroyed. This is the last step in the component lifecycle, it is similar to death . 
> This hook is not called during server-side rendering



If you ever feel lost about Vue JS lifecycle hooks you can have a look at the [lifecycle diagram] (https://v3.vuejs.org/guide/instance.html#lifecycle-diagram)