{"id":8516,"date":"2023-08-14T10:07:15","date_gmt":"2023-08-14T10:07:15","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=8516"},"modified":"2023-08-14T10:07:15","modified_gmt":"2023-08-14T10:07:15","slug":"nexttick-in-vuejs","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/vue\/nexttick-in-vuejs","title":{"rendered":"What is nextTick and what does it do in Vue.js?"},"content":{"rendered":"<p>In Vue.js, the nextTick method is used to schedule a callback to be executed after the next DOM update cycle.<\/p>\n<p>It allows you to perform operations after the Vue instance has finished updating the DOM, ensuring that you&#8217;re working with the latest changes. This is particularly useful when you want to manipulate the DOM or access the updated values immediately after a data change.<\/p>\n<p><strong>Here&#8217;s an example to illustrate the use case of nextTick in Vue.js:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;template&gt;\r\n  &lt;div&gt;\r\n    &lt;p&gt;{{ message }}&lt;\/p&gt;\r\n    &lt;button @click=\"updateMessage\"&gt;Update Message&lt;\/button&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/template&gt;\r\n\r\n\r\n&lt;script&gt;\r\nexport default {\r\n  data() {\r\n    return {\r\n      message: \"Initial message\"\r\n    };\r\n  },\r\n  methods: {\r\n    updateMessage() {\r\n      this.message = \"New message\"; \/\/ Update the data property\r\n      const paragraph = document.querySelector('p');\r\n      console.log(paragraph.textContent); \/\/ Outputs: Initial message\r\n    }\r\n  }\r\n};\r\n&lt;\/script&gt;<\/pre>\n<p>In the above example, we have a Vue component with a message data property. When the &#8220;Update Message&#8221; button is clicked, the updateMessage method is called, which updates the message property with a new value.<\/p>\n<p>But when we try to access an element using <strong>document.querySelector()<\/strong>, We are getting an old value because Vue hasn&#8217;t finished updating the DOM.<\/p>\n<p><strong>Here&#8217;s an example how to get updated DOM result using nextTick:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;template&gt;\r\n  &lt;div&gt;\r\n    &lt;p&gt;{{ message }}&lt;\/p&gt;\r\n    &lt;button @click=\"updateMessage\"&gt;Update Message&lt;\/button&gt;\r\n  &lt;\/div&gt;\r\n&lt;\/template&gt;\r\n\r\n\r\n&lt;script&gt;\r\nexport default {\r\n  data() {\r\n    return {\r\n      message: \"Initial message\"\r\n    };\r\n  },\r\n  methods: {\r\n    updateMessage() {\r\n      this.message = \"New message\"; \/\/ Update the data property\r\n\r\n\r\n      this.$nextTick(() =&gt; {\r\n        \/\/ This callback will be executed after the next DOM update cycle\r\n        \/\/ Accessing the DOM or data here ensures that it reflects the latest changes\r\n\r\n\r\n        const paragraph = document.querySelector('p');\r\n        console.log(paragraph.textContent); \/\/ Outputs: New message\r\n      });\r\n    }\r\n  }\r\n};\r\n&lt;\/script&gt;\r\n<\/pre>\n<p>In the above example, Immediately after updating the data property, <strong>this.$nextTick()<\/strong> is called with a callback function. Inside the callback, we access the DOM to get the text content of the < p > element, which displays the message value. Since <strong>this.$nextTick()<\/strong> ensures that the DOM has been updated with the latest changes, we can be sure that the text content will reflect the updated value of the message.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In Vue.js, the nextTick method is used to schedule a callback to be executed after the next DOM update cycle. It allows you to perform operations after the Vue instance has finished updating the DOM, ensuring that you&#8217;re working with the latest changes. This is particularly useful when you want to manipulate the DOM or [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":8518,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[13],"tags":[],"class_list":["post-8516","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-vue"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8516"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=8516"}],"version-history":[{"count":3,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8516\/revisions"}],"predecessor-version":[{"id":8520,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/8516\/revisions\/8520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/8518"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=8516"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=8516"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=8516"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}