<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Asad Kaleem on Medium]]></title>
        <description><![CDATA[Stories by Asad Kaleem on Medium]]></description>
        <link>https://medium.com/@asadkaleem-60451?source=rss-3049a158d0d0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*l6y2poxhc1OyRc-U</url>
            <title>Stories by Asad Kaleem on Medium</title>
            <link>https://medium.com/@asadkaleem-60451?source=rss-3049a158d0d0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 15:46:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@asadkaleem-60451/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[React-Native under the hood state implementation]]></title>
            <link>https://asadkaleem-60451.medium.com/react-native-under-the-hood-state-implementation-24acc2c41cdc?source=rss-3049a158d0d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/24acc2c41cdc</guid>
            <category><![CDATA[react]]></category>
            <category><![CDATA[react-native]]></category>
            <dc:creator><![CDATA[Asad Kaleem]]></dc:creator>
            <pubDate>Sun, 25 Oct 2020 08:40:22 GMT</pubDate>
            <atom:updated>2020-10-25T08:40:22.851Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mm8JucFufPWr2ls_I1h_0w.png" /><figcaption>React Native</figcaption></figure><p>We are currently in an era where there is a flux of javascript libraries/frameworks, every turned stone reveals a new library or framework and we start ourselves pushing towards the new reveal. Somehow in this competitive environment React and React-Native paved their way towards the top of the chain.</p><p>This blog will describe how state change is handled in React-Native.</p><p>React concepts are prerequisites for understanding the concepts of React-Native state change. I strongly suggest that if you have a knowledge gap there then you should learn about that first then start reading this blog.</p><h4><strong>React Implementation of state</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Wsht669HCjD7Ekjmvmky4g.png" /><figcaption>Count function</figcaption></figure><p>This is a simple React function that has a section that displays the current count and a button that increments the count which will further update the display count in the section, <strong>but how does React do it?</strong></p><p>React keeps track of all the components in a tree and memoizes the tree, if there is state change or any action that causes the re-render, React refreshes the tree and compares it with the memoized tree. This step isolates the components that need re-rendering. React further commits the changes that are needed in these components. These commits further make the changes in the actual DOM.</p><h4>React-Native implementation of state</h4><p>Before getting into state changes we must learn about the concept of <strong>The Bridge</strong> in React-Native.</p><p><strong>The Bridge</strong> is a concept that provides a way for bidirectional and asynchronous communications between the Javascript realm and the Native Realm.</p><p>There is a lot of information about the implementation of <strong>The Bridge</strong> on the internet if you require detailed information. I’ve shared the link of a blog at the end of this blog which helped me.</p><p>There are two realms to make React-Native apps work. One is the Javascript realm which is our side where we code and the other is the Native realm(Kotlin, Obj-C, Swift, Java) which is used to display what we coded. Obviously, there must be some mechanism to communicate between these two realms, there comes the concept of <strong>The Bridge</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*um91ZoTCcIoWtSzdoPmPOA.png" /><figcaption>React-Native function</figcaption></figure><p>This is actually the part of React that works the same way in React-Native. It does the rendering stuff then it finds the differences and then updates the rendering backend. How rendering happens in React is explained above but how React-Native does the rendering is the real question because we don’t have DOM, we cannot change through ‘element by id’ and we cannot just call the native functions because they are costly. Let&#39;s understand this through an example.</p><p>Let&#39;s suppose onPressHandler looks like the below image</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*YBSrtIN_5Q2tA_rhYa3Wlg.png" /><figcaption>press handler</figcaption></figure><p>When the onPress handler is called the Native side select methods that are called onPress along with their params, serialize it, and pass it down to the React-Native side. The React-Native side receives it in the form of a JSON object. React-Native computes the changes convert them into small instructions and add them to Javascript Queue.</p><p>Queue for the above function will look like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jTNUe3aFDDM4xZHgUMiOag.png" /></figure><p>This queue is further serialized and passed down to the Native side through <strong>The Bridge</strong>. Both sides queue will look like the below images:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*8VLFlfxvl-3b9LCQWhxlhQ.png" /><figcaption>JS queue</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*FR6A7U0EHAwaKFyXaEMitg.png" /><figcaption>Native Queue</figcaption></figure><p>The above two images describe two instructions for the native side to follow, one is to update the UI and the second is to make an API call to the given URL. The interesting thing in the above images is the two managers that are being used to operate on these commands.</p><p><strong>1- UI Manager:</strong><br>UI Manager takes these commands from React-Native and applies them on Native platforms. It schedules these operations on the main thread (Native Queue). UI changes can only be done through the main thread. At the end of this process UI Manager will mark these UI elements as dirty.</p><p>UI Manager will further send these instructions to <strong>Layouts </strong>which then climbs up the tree and recalculates the layout.</p><p><strong>2- Data Manager:</strong><br>Data Manager is just a polyfill above the fetch spec to make API calls, under the hood which only makes any API call like Axios or Fetch.</p><p>The final image of the whole flow is below:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NK7qjCGFSsdBQYS4MYWbOA.png" /><figcaption>Flow from React-Native to Native</figcaption></figure><p>To summarise all the above when the button is pressed on UI, Native sends the method and its parameters to React-Native. React-Native then computes the changes and tell the Native side the changes. All of this communication is done through The Bridge.</p><p>This blog has three main concepts detailed explanation can be gathered from these blogs:<br>1- <strong>The Bridge:</strong> <a href="https://hackernoon.com/understanding-react-native-bridge-concept-e9526066ddb8">https://hackernoon.com/understanding-react-native-bridge-concept-e9526066ddb8</a><br>2- <strong>State Change is handled through the Native side: </strong><a href="http://youtube.com/watch?v=hDviGU-57lU&amp;t=1344s">youtube.com/watch?v=hDviGU-57lU&amp;t=1344s</a><br>3- <strong>Threads in the whole process:</strong> <a href="https://www.reactnative.guide/3-react-native-internals/3.1-react-native-internals.html">https://www.reactnative.guide/3-react-native-internals/3.1-react-native-internals.html</a></p><p>That’s it!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24acc2c41cdc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Reactive way of using D3]]></title>
            <link>https://asadkaleem-60451.medium.com/reactive-way-of-using-d3-1e4a65ba9ae?source=rss-3049a158d0d0------2</link>
            <guid isPermaLink="false">https://medium.com/p/1e4a65ba9ae</guid>
            <category><![CDATA[d3js]]></category>
            <category><![CDATA[react]]></category>
            <category><![CDATA[reactjs]]></category>
            <dc:creator><![CDATA[Asad Kaleem]]></dc:creator>
            <pubDate>Mon, 03 Aug 2020 12:59:48 GMT</pubDate>
            <atom:updated>2020-08-03T12:59:48.582Z</atom:updated>
            <content:encoded><![CDATA[<p>Reactive way of using D3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-8Dcqus9OXfKMtuFmLtjpw.png" /></figure><p>React community is divided into 2 groups, one who thinks manipulating DOM without involving React is an act of treason and second who thinks first group is full of crap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/1*5Kh-PwE4RuDiC65JSsLJyw.gif" /></figure><p>There are two ways of D3 integration with React:</p><p>1- Use D3 for its functions and DOM manipulation<br>2- Use D3 for its functions and let React do the DOM manipulation</p><p>This post is for newbies who thinks React and D3 together in a reactive way are complex.</p><p>I will explain a very basic example which will create below chart. So lets Begin!</p><figure><img alt="This chart can be accessed through this link http://bl.ocks.org/enjalot/1203641" src="https://cdn-images-1.medium.com/max/219/1*A_WGZ15Wbw4uEa6WMbobJQ.png" /><figcaption>http://bl.ocks.org/<a href="http://bl.ocks.org/enjalot/1203641">enjalot</a>/1203641</figcaption></figure><p>This chart is result of multiple pies joined together. Each pie will be created through svg elements:</p><p>1- &lt;g&gt; element will group the text and their respective arcs. Just like &lt;div&gt; in simple html elements. <br>2- &lt;text&gt; will create a text eleenterment to display the labels.<br>3- &lt;path&gt; element actually creates the arcs</p><p>Combination of these elements will create above pie chart.</p><p><strong>Implementation 1 (Mutating DOM):</strong></p><p>1- <a href="https://www.d3indepth.com/selections/#:~:text=select%20selects%20the%20first%20matching,with%20class%20item%20use%20d3."><strong><em>selectAll</em></strong></a> function finds all elements for given selectors in DOM.<br>2- <a href="https://bl.ocks.org/uicoded/dec8786d89184c88fa8f2c0abcdc152d"><strong><em>append</em></strong></a> function appends elements as a last child in the selected element.<br>3- <a href="https://www.tutorialsteacher.com/d3js/function-of-data-in-d3js"><strong><em>data</em></strong></a> function takes data points to run functions on them and give data to the selected elements.<br>4- <a href="http://attr function will add attributes on the element like inline-styling"><strong><em>enter</em></strong></a> function joins our data with the elements. <br>5- <strong><em>centeroid </em></strong>function provides the center point of the arc to append text on this position<br>6- <strong><em>attr</em></strong> function will add attributes on the element like inline-styling.</p><p>There are two more functions <strong>arc</strong> and <strong>pie</strong> that are explained in next section as they are common functions in both implementations.</p><pre>var arcs = vis.selectAll(&quot;g.slice&quot;)    <br>              .data(pie)       <br>              .enter()   <br>              .append(&quot;svg:g&quot;) <br>              .attr(&quot;class&quot;, &quot;slice&quot;); </pre><pre>var arc = d3.svg.arc()              <br>           .outerRadius(r);<br><br>var pie = d3.layout.pie()           <br>           .value(function(d) { return d.value; });</pre><p>This chunk of code will take pie generated data run map function on it and will append &lt;g&gt; tag in &lt;svg&gt; element for each iteration with class <strong>slice</strong>. This will create three&lt;g&gt; elements if data length is three.</p><pre>arcs.append(&quot;svg:path&quot;)<br>  .attr(&quot;fill&quot;, <em>function</em>(<em>d</em>, <em>i</em>) {<br>     return color(<em>i</em>);<br>   })<br>  .attr(&quot;d&quot;, arc);</pre><pre>arcs.append(&quot;svg:text&quot;)<br>  .attr(&quot;transform&quot;, <em>function</em>(<em>d</em>) {<br>     <em>d</em>.innerRadius = 0;<br>     <em>d</em>.outerRadius = r;<br>     return &quot;translate(&quot; + arc.centroid(<em>d</em>) + &quot;)&quot;; <br>  })<br>  .attr(&quot;text-anchor&quot;, &quot;middle&quot;)<br>  .text(<em>function</em>(<em>d</em>, <em>i</em>) {<br>  return data[<em>i</em>].label;<br>});</pre><p>Above chunks of code will find and select &lt;svg&gt; element from DOM and append &lt;g&gt;, &lt;path&gt;, &lt;text&gt; in it, without involvement of React. You can review full code from the link of the image.</p><p>Text will be displayed in middle of each arc because of centriod function.</p><p>Final DOM elements will look like this:</p><pre>&lt;svg width=”300&quot; height=”300&quot;&gt;<br> &lt;g transform=”translate(100,100)”&gt;<br>  &lt;g class=”slice”&gt;<br>  &lt;path fill=”#3182bd” d=”” /&gt;<br>  &lt;text transform=”translate()” text-anchor=”middle”&gt;<br>   one<br>  &lt;/text&gt;<br>  &lt;/g&gt;<br>  &lt;g class=”slice”&gt;<br>  &lt;path fill=”#6baed6” d=”” /&gt;<br>  &lt;text transform=”translate()” text-anchor=”middle”&gt;<br>   two<br>  &lt;/text&gt;<br>  &lt;/g&gt;<br>  &lt;g class=”slice”&gt;<br>  &lt;path fill=”#9ecae1” d=”” /&gt;<br>  &lt;text transform=”translate()” text-anchor=”middle”&gt;<br>   three<br>  &lt;/text&gt;<br>  &lt;/g&gt;<br> &lt;/g&gt;<br>&lt;/svg&gt;</pre><pre>1- arc function generator produces a circular or annular sector, as in a pie or donut chart. It also decides the outer radius or inner radius of the chart. There are other functions attached with arc as well.<br>2- pie generator does not produce a shape directly, but instead computes the necessary angles to represent a tabular dataset as a pie or donut chart</pre><p>Now looking at above markup it can easily by created through js map function and d3 functions.</p><p><strong>Implementation 2: (Reactive way)</strong></p><p>Reactive implementation is as simple as writing JSX. You just need basic knowledge of svg elements.</p><p>Above functions are also used in d3 implementation. These are the pure d3 functions that we need for calculations. Other selection functions are not needed in reactive implementation of chart.</p><p>Above code will be converted into:</p><pre><em>const</em> data = [<br>  { label: &quot;one&quot;, value: 20, color: &quot;yellow&quot; },<br>  { label: &quot;two&quot;, value: 50, color: &quot;blue&quot; },<br>  { label: &quot;three&quot;, value: 30, color: &quot;red&quot; },<br>];<br>// D3 Functions<br><em>const</em> fullArc = arc().outerRadius(100).innerRadius(0);<br><em>const</em> pieGenerator = d3.pie&lt;<em>any</em>&gt;().value((<em>d</em>: <em>any</em>) <em>=&gt;</em> <em>d</em>.value);<br><em>const</em> arcs = pieGenerator(data);</pre><pre>// React Implementation of DOM<br>return (<br>&lt;div className=&quot;App&quot;&gt;<br>  &lt;svg width=&quot;300&quot; height=&quot;300&quot;&gt;<br>    &lt;g transform=&quot;translate(100,100)&quot;&gt;<br>      {arcs.map((<em>obj</em>: <em>any</em>, <em>index</em>: <em>number</em>) <em>=&gt;</em> {<br>      <em>const</em> arcDimention = fullArc(<em>obj</em>);<br>      return (<br>      &lt;g className=&quot;slice&quot;&gt;<br>        &lt;path fill={data[<em>index</em>].color} d={`${arcDimention}`} /&gt;<br>        &lt;text transform={`translate(${fullArc.centroid(<em>obj</em>)})`} <br>        text-anchor=&quot;middle&quot;&gt;<br>           {<em>obj</em>.value}<br>        &lt;/text&gt;<br>      &lt;/g&gt;);<br>  })}<br>  &lt;/g&gt;<br>  &lt;/svg&gt;<br>&lt;/div&gt;<br>);</pre><p>In above code there are two comments which will explain the functions we are using of d3 and React implementation of markup. That’s It!!</p><p>Reactive way of implementing d3 is as simple as that.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1e4a65ba9ae" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>