<?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: Himanshu Singh</title>
    <description>The latest articles on DEV Community by Himanshu Singh (@tinypixel_).</description>
    <link>https://dev.to/tinypixel_</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%2F1078347%2F2e5dc0a6-eb2a-493a-9321-32d7b656c0fc.jpg</url>
      <title>DEV Community: Himanshu Singh</title>
      <link>https://dev.to/tinypixel_</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/tinypixel_"/>
    <language>en</language>
    <item>
      <title>Deep-dive into Reactivity - Part 1</title>
      <dc:creator>Himanshu Singh</dc:creator>
      <pubDate>Sat, 03 Jun 2023 11:36:35 +0000</pubDate>
      <link>https://dev.to/tinypixel_/deep-dive-into-reactivity-part-1-372g</link>
      <guid>https://dev.to/tinypixel_/deep-dive-into-reactivity-part-1-372g</guid>
      <description>&lt;p&gt;Welcome to the easy world. In this world, we just have to &lt;em&gt;update a variable&lt;/em&gt;, and the framework does the heavy lifting of:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Tracking&lt;/strong&gt; all the places this variable was used&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Re-computing&lt;/strong&gt; the &lt;em&gt;dependent functions&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Updating&lt;/strong&gt; the &lt;em&gt;dependent DOM element&lt;/em&gt;, in the most efficient way possible.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's take a simple example. Go ahead and &lt;strong&gt;click&lt;/strong&gt; that button.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/react-click-update-7mz19k?view=split"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;On click of the button&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;React has to &lt;strong&gt;update the variable&lt;/strong&gt; &lt;code&gt;name&lt;/code&gt; to Batman.&lt;/li&gt;
&lt;li&gt;As &lt;code&gt;name&lt;/code&gt; was used in the DOM, it has to iterate over the DOM and &lt;strong&gt;update the exact h1 tag&lt;/strong&gt; (without an &lt;code&gt;id&lt;/code&gt; to &lt;code&gt;getElementById&lt;/code&gt; on).&lt;/li&gt;
&lt;li&gt;Also as &lt;code&gt;name&lt;/code&gt; was part of &lt;code&gt;useEffect&lt;/code&gt;'s dependency array, it will &lt;strong&gt;trigger the callback function&lt;/strong&gt;, and update the DOM element with the clicked timestamp.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;No big deal, right? &lt;/p&gt;

&lt;p&gt;Let's try and implement this in Vanilla JS from scratch to see why this is a big deal.&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/thirsty-sanne-99yf5o?view=editor"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Pheew 😪. You saw all the &lt;strong&gt;extraHardWork()&lt;/strong&gt; we had to do to get it working?&lt;/p&gt;

&lt;p&gt;As a developer our core job should be to &lt;em&gt;develop the app&lt;/em&gt;. The more work we delegate to the framework, the better we can do our job.&lt;/p&gt;

&lt;p&gt;In this case, the time we spend tracking &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;which DOM elements to update&lt;/li&gt;
&lt;li&gt;in which order should it be executed&lt;/li&gt;
&lt;li&gt;assigning &lt;code&gt;id&lt;/code&gt; to each dynamic DOM element, and updating it.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;is the time spent not doing our core job.&lt;/p&gt;

&lt;p&gt;As the size of the applications scales, this leads to increasing number of bugs. Feeling grateful yet? 😇&lt;/p&gt;

&lt;p&gt;In &lt;strong&gt;Part 2&lt;/strong&gt; we get into how various frameworks like React, Vue and Svelte implement &lt;strong&gt;Reactivity&lt;/strong&gt;, and what kind of crazy optimizations they do to keep it performant.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>frontend</category>
      <category>react</category>
    </item>
  </channel>
</rss>
