<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	
	xmlns:georss="http://www.georss.org/georss"
	xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#"
	>

<channel>
	<title>vue.js Archives - Program Easily</title>
	<atom:link href="https://programeasily.com/tag/vue-js/feed/" rel="self" type="application/rss+xml" />
	<link>https://programeasily.com/tag/vue-js/</link>
	<description>Program Easily helps people to learn about software programs in a easy manner.</description>
	<lastBuildDate>Thu, 15 Dec 2022 03:54:10 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.8.1</generator>

<image>
	<url>https://i0.wp.com/programeasily.com/wp-content/uploads/2020/12/cropped-logo.png?fit=32%2C32&#038;ssl=1</url>
	<title>vue.js Archives - Program Easily</title>
	<link>https://programeasily.com/tag/vue-js/</link>
	<width>32</width>
	<height>32</height>
</image> 
<site xmlns="com-wordpress:feed-additions:1">187075990</site>	<item>
		<title>When to use NextTick API in Vue.js</title>
		<link>https://programeasily.com/2022/12/15/when-to-use-nexttick-api-in-vue-js/</link>
					<comments>https://programeasily.com/2022/12/15/when-to-use-nexttick-api-in-vue-js/#respond</comments>
		
		<dc:creator><![CDATA[Admin]]></dc:creator>
		<pubDate>Thu, 15 Dec 2022 03:54:10 +0000</pubDate>
				<category><![CDATA[Vuejs]]></category>
		<category><![CDATA[vue]]></category>
		<category><![CDATA[vue.js]]></category>
		<category><![CDATA[vue3]]></category>
		<guid isPermaLink="false">https://programeasily.com/?p=2042</guid>

					<description><![CDATA[<p>NextTick is one of the cool feature in the VUE world. In Vue.js, When we change the state, it will not update/affect the DOM instantly or synchronously. Rather It will collect the state changes and do the DOM update once asynchronously no matter how many state changes you have made. NextTick API in Vue.js &#60;script&#62; import { nextTick } from 'vue' export default { data() { return { count: 0 } }, methods: { async increment() { this.count++ // DOM...</p>
<p class="read-more"><a class="btn btn-default" href="https://programeasily.com/2022/12/15/when-to-use-nexttick-api-in-vue-js/"> Read More<span class="screen-reader-text">  Read More</span></a></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2022/12/15/when-to-use-nexttick-api-in-vue-js/">When to use NextTick API in Vue.js</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><a href="https://vuejs.org/api/general.html#nexttick">NextTick</a> is one of the cool feature in the VUE world. In <a href="https://programeasily.com/vue/">Vue.js</a>, When we change the state, it will not update/affect the DOM instantly or synchronously. Rather It will collect the state changes and do the DOM update once asynchronously no matter how many state changes you have made.</p>
<p><img fetchpriority="high" decoding="async" class="size-full wp-image-2046" src="https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?resize=640%2C360&#038;ssl=1" alt="NextTick API" width="640" height="360" srcset="https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?w=1920&amp;ssl=1 1920w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?resize=300%2C169&amp;ssl=1 300w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?resize=1024%2C576&amp;ssl=1 1024w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?resize=768%2C432&amp;ssl=1 768w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?resize=1536%2C864&amp;ssl=1 1536w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?resize=480%2C270&amp;ssl=1 480w, https://i0.wp.com/programeasily.com/wp-content/uploads/2022/12/Snapshot_58.png?w=1280&amp;ssl=1 1280w" sizes="(max-width: 640px) 100vw, 640px" data-recalc-dims="1" /></p>
<h2>NextTick API in Vue.js</h2>
<pre><code class="language-javascript">&lt;script&gt;
import { nextTick } from 'vue'

export default {
  data() {
    return {
      count: 0
    }
  },
  methods: {
    async increment() {
      this.count++

      // DOM not yet updated
      console.log(document.getElementById('counter').textContent) // 0
    }
  }
}
&lt;/script&gt;

&lt;template&gt;
  &lt;button id="counter" @click="increment"&gt;{{ count }}&lt;/button&gt;
&lt;/template&gt;</code></pre>
<p>In the above program, we are changing the state of count by doing count++. But this will not update the DOM instantly rather the vue.js frameworks collects this information in the backend. After the whole execution of <code>increment()</code> method, It will update the DOM asynchronously.</p>
<p>But the problem is <code>document.getElementById('counter').textContent)</code> returns 0 in the <code>increment()</code> method. so if someone wants to use DOM element immediately, which is not possible in Vue.js directly. That&#8217;s where we have to use nextTick global API. <a href="https://v2.vuejs.org/v2/api/#Vue-nextTick" rel="noreferrer"><code>nextTick</code></a> allows you to execute code <em>after</em> you have changed some data and Vue.js has updated the virtual DOM based on your data change, but <em>before</em> the browser has rendered that change on the page.</p>
<p>We can pass the callback function to the <code>nextTick(callback(){})</code> which can be triggered after the successful DOM update. You can either pass a callback as an argument, or await the returned Promise.</p>
<pre><code class="language-javascript">import { nextTick } from 'vue'

export default {
  methods: {
    increment() {
      this.count++
      nextTick(() =&gt; {
        // access updated DOM
      })
    }
  }
}</code></pre>
<h2>Why Not SetTimeout ?</h2>
<p>Some people would think, why not <code>setTimeOut</code>!. We can also use <code>setTimeOut</code> But the problem is When you invoke <code>setTimeout</code>…</p>
<pre><code class="language-javascript">setTimeout(function() { 
    // access updated DOM
}, 100);</code></pre>
<p>then the browser would have a chance to update the page, and <em>then</em> your callback would get called. So the UI will render the old record first and then the new record will come. It&#8217;s not look good right. But with the help of <code>nextTick()</code> we can change the DOM value before being render into the screen. That&#8217;s the beauty of this API in Vue.js</p>
<h2>Summary</h2>
<p><code>nextTick()</code> can be used immediately after a state change to wait for the DOM updates to complete. We should not use <code>setTimeOut</code>to update the DOM rather should prefer <code>nextTick()</code></p>
<p>The post <a rel="nofollow" href="https://programeasily.com/2022/12/15/when-to-use-nexttick-api-in-vue-js/">When to use NextTick API in Vue.js</a> appeared first on <a rel="nofollow" href="https://programeasily.com">Program Easily</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://programeasily.com/2022/12/15/when-to-use-nexttick-api-in-vue-js/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
		<post-id xmlns="com-wordpress:feed-additions:1">2042</post-id>	</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/

Page Caching using disk: enhanced 
Minified using disk
Database Caching using disk (Request-wide modification query)

Served from: programeasily.com @ 2025-06-30 07:04:34 by W3 Total Cache
-->