<?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>Forem: Raibima Putra</title>
    <description>The latest articles on Forem by Raibima Putra (@raibima).</description>
    <link>https://forem.com/raibima</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%2F100813%2F3b233c14-73a6-44cf-9760-9c61fc1c79a3.jpg</url>
      <title>Forem: Raibima Putra</title>
      <link>https://forem.com/raibima</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/raibima"/>
    <language>en</language>
    <item>
      <title>Batch Your React Updates</title>
      <dc:creator>Raibima Putra</dc:creator>
      <pubDate>Sun, 12 Jul 2020 00:25:30 +0000</pubDate>
      <link>https://forem.com/raibima/batch-your-react-updates-120b</link>
      <guid>https://forem.com/raibima/batch-your-react-updates-120b</guid>
      <description>&lt;p&gt;React doesn't automatically batch updates that you trigger in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Native DOM event listeners (e.g., &lt;code&gt;el.addEventListener()&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Async callbacks (e.g., promises, timeouts, intervals)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Consider the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In the above example, React will re-render your UI tree twice! &lt;a href="https://codesandbox.io/s/festive-monad-d6eld?file=/src/App.js"&gt;Try it on CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One way to fix this is to use a single state variable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="nx"&gt;fetchProfile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;userId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(({&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;age&lt;/span&gt;&lt;span class="p"&gt;})&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// now instead of using two state&lt;/span&gt;
    &lt;span class="c1"&gt;// variables, we just use one.&lt;/span&gt;
    &lt;span class="nx"&gt;setUserProfile&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="nx"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="nx"&gt;age&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://codesandbox.io/s/sleepy-galois-rurjp?file=/src/App.js"&gt;Try it on CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Another way is to wrap updates inside &lt;code&gt;unstable_batchedUpdates&lt;/code&gt; callback. As the name suggests, this API will batch your updates in a single reconciliation pass, resulting in less component renders.&lt;/p&gt;

&lt;p&gt;Don't worry, the API is very stable despite the &lt;code&gt;unstable_&lt;/code&gt; prefix. The only thing to keep in mind is that the API is renderer-specific, meaning that on the web it should be imported from the &lt;code&gt;react-dom&lt;/code&gt; package, not &lt;code&gt;react&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;unstable_batchedUpdates&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-dom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// react-native&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;unstable_batchedUpdates&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;react-native&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Then, use it like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="nx"&gt;unstable_batchedUpdates&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;setName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;John&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nx"&gt;setAge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;a href="https://codesandbox.io/s/compassionate-shamir-tl7fz?file=/src/App.js"&gt;Try it on CodeSandbox&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;You might be thinking that this is pretty unintuitive and verbose. I sometimes wonder myself why this isn't automatic. The good news is that React will make it so in a future release, but we need to first opt-in to either &lt;a href="https://reactjs.org/docs/concurrent-mode-adoption.html#feature-comparison"&gt;Blocking Mode&lt;/a&gt;, or &lt;a href="https://reactjs.org/docs/concurrent-mode-adoption.html#feature-comparison"&gt;Concurrent Mode&lt;/a&gt;. In the meantime, try these workarounds!&lt;/p&gt;

</description>
      <category>react</category>
      <category>web</category>
    </item>
  </channel>
</rss>
