<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Kolly</title>
    <description>The latest articles on DEV Community by Kolly (@kolly).</description>
    <link>https://dev.to/kolly</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1372482%2Fdf0b6389-2b11-403f-9565-915aeaafb327.jpeg</url>
      <title>DEV Community: Kolly</title>
      <link>https://dev.to/kolly</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/kolly"/>
    <language>en</language>
    <item>
      <title>A guide to Vue Lifecycle hooks.</title>
      <dc:creator>Kolly</dc:creator>
      <pubDate>Fri, 22 Mar 2024 05:27:22 +0000</pubDate>
      <link>https://dev.to/kolly/a-guide-to-vue-lifecycle-hooks-414a</link>
      <guid>https://dev.to/kolly/a-guide-to-vue-lifecycle-hooks-414a</guid>
      <description>&lt;p&gt;&lt;strong&gt;What you will learn&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://vuejs.org/api/options-state.html"&gt;State options: &lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://vuex.vuejs.org/"&gt;Vuex&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vue js lifecycle hooks&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;code&gt;beforeCreate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;created&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;beforeMount&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;mounted&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;beforeUpdate&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;updated&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;beforeUnmount&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;unmounted&lt;/code&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Let's take a closer look at how and when to use these hooks.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;beforeCreate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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).&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;beforeCreate(){
  console.log('instanced initialized')
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;created&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;created(){
  console.log("is Processed state options'")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;beforeMount&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;beforeMount(){
   console.log("before mount")   
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Mounted&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mounted(){
    console.log("mounted")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;beforeUpdate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;beforeUpdated(){
    console.log("before component update")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;updated&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;updated(){
    console.log("updated");
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;beforeUnmount&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This hook is called right before a component is unmounted, while the component instance is still active and working efficiently.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;beforeUnmount(){
   console.log("before unmount")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;unmounted&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;unmounted(){
   console.log("component unmounted")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Conclusion&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;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!!!&lt;/p&gt;

&lt;p&gt;Stay tune for my next blog series, Rudiments.&lt;/p&gt;

</description>
      <category>vue</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
