<?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: arnabchat90</title>
    <description>The latest articles on Forem by arnabchat90 (@arnabchat90).</description>
    <link>https://forem.com/arnabchat90</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%2F326188%2F9b7e06d1-4bff-421c-a76a-f3d6f59c7a7f.png</url>
      <title>Forem: arnabchat90</title>
      <link>https://forem.com/arnabchat90</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/arnabchat90"/>
    <language>en</language>
    <item>
      <title>React Internals - React Fiber Introduction</title>
      <dc:creator>arnabchat90</dc:creator>
      <pubDate>Mon, 29 Nov 2021 07:41:21 +0000</pubDate>
      <link>https://forem.com/arnabchat90/react-internals-react-fiber-introduction-2ba8</link>
      <guid>https://forem.com/arnabchat90/react-internals-react-fiber-introduction-2ba8</guid>
      <description>&lt;p&gt;Being declarative is a good thing and probably the best about React. But like all good things there are few things that are not so nice about being declarative, because here &lt;strong&gt;you&lt;/strong&gt; as a consumer of the library instruct react about &lt;em&gt;what to do&lt;/em&gt;, and not bother about how it actually does it, which then makes it ok not to ever know goes under the hood.&lt;/p&gt;

&lt;p&gt;Generally if you use react the way its supposed to be used, you would not need to know how react does what it does for us. But as you write more applications you would realize that understanding some of the internals of react will actually help you to write better code. Use react the way it was intended, rather than use a hack of some sort and then later regret it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Virtual DOM
&lt;/h3&gt;

&lt;p&gt;Most people while learning react, learn about the fact that when ever there is a state or prop update, the components re-render, and that changes are flushed to the UI(dom), and for obvious performance reasons, react does not always tear down and recreate the dom on every state change. That's when the concept of virtual dom comes in, react on every render creates an immutable tree of react elements from various type of components with the current state and props, whenever there is an update, it creates an updated react element tree. And then runs a diffing algorithm to transform the tree from one state to another.&lt;/p&gt;

&lt;p&gt;Please read through the assumptions react makes and a high level take on the diffing algorithm used by react -&lt;/p&gt;

&lt;p&gt;React Reconciliation Official Docs &lt;a href="https://reactjs.org/docs/reconciliation.html"&gt;React Reconciliation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This immutable tree of react elements was called the virtual dom before, but now the term is no longer used by the React team in their official documentation. I will call this the react element tree going forward.&lt;/p&gt;

&lt;h3&gt;
  
  
  Changes in React 16
&lt;/h3&gt;

&lt;p&gt;React also used to keep an internal tree of instances, which are used to track state, in version 16 they introduced the concept of &lt;strong&gt;Fiber Nodes&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Fiber nodes constitute the internal tree that tracks state, props and effects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;em&gt;From the moment state updates inside a component, to the moment actual dom changes, work that react does to achieve it is called reconciliation&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is the primary topic we are going to cover in-depth and in plain English with help of code snippets screenshots and diagrams for better and clear understanding.&lt;/p&gt;

&lt;h3&gt;
  
  
  Motivation for Fiber Architecture
&lt;/h3&gt;

&lt;p&gt;React adopted the Fiber architecture for performance reasons, earlier the reconciliation process was completely synchronous, but in react fiber the reconciliation process is divided into two phases - &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Render&lt;/li&gt;
&lt;li&gt;Commit&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will come to what happens in each phase, but for now just remember that these two phases are asynchronous.&lt;br&gt;
So the earlier issues with long running tasks causing frame drops and making the app laggy no longer exist.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reconciliation Process - 10,000 Ft view
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create the &lt;strong&gt;Fiber Root&lt;/strong&gt; from the &lt;strong&gt;root container element&lt;/strong&gt; (when using ReactDom.render, createRoot is the new way of doing it, not part of this article) - &lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--WP3YC1tY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btkasgjbje0vdguyidng.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--WP3YC1tY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/btkasgjbje0vdguyidng.png" alt="fiber root" width="860" height="716"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;React would create a react element object starting from the top level component&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hNh20Q-I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnq47jiqwcri0gy5gjy8.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hNh20Q-I--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cnq47jiqwcri0gy5gjy8.png" alt="react element object" width="880" height="289"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;For each and every react element(could be functional, class, DOM/Host component etc) there would be a fiber node created and is added to the Fiber tree(you would be surprised that this tree is not actually a tree but a linked list!!)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This fiber tree, &lt;strong&gt;&lt;em&gt;basically acts as a mutable data structure&lt;/em&gt;&lt;/strong&gt;, which keeps track of work to be performed on each of the fiber node, now work for each element might be different based on the component type, it might be firing component life cycle events, flushing updates to dom, calling a side effect etc.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So after the fiber node tree is created, &lt;strong&gt;&lt;em&gt;in the render phase&lt;/em&gt;&lt;/strong&gt;, for all the fiber nodes, the react framework, performs the work needed in each of these nodes and traverses the entire tree on child and siblings to make sure all the work defined in each node is performed, remember that no side effects are run during this phase and that these unit of work in each node is asynchronous, can be paused, scraped and redone.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Last phase is the &lt;strong&gt;&lt;em&gt;commit phase&lt;/em&gt;&lt;/strong&gt;, this is where the post render life cycle events, DOM updates and other side effect which can affect dom directly takes place, this phase hence has to be synchronous.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Once the commit phase is done, one cycle of the reconciliation phase is completed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Below is a high level scribble for when ReactDOM.render is called for the first time and how the reconciliation kicks in.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--bRhL8Pjk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4boanifollaas59quab1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--bRhL8Pjk--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4boanifollaas59quab1.png" alt="reconciliation" width="880" height="587"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;On a broad level, this shows the initial creation of Fiber root, and then how the fiber tree is created based on the react element objects, and then work is performed on each of the node as part of the render phase.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is where I stop for this introduction, I will come up with a more in-depth view where I would debug the whole cycle from updating an element and how finally dom changes are triggered and flushed.&lt;/p&gt;

&lt;p&gt;But that's for another day, please do leave feedback below.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
    </item>
    <item>
      <title>Debugging React Source Code with a React Client App</title>
      <dc:creator>arnabchat90</dc:creator>
      <pubDate>Sat, 17 Jul 2021 08:57:07 +0000</pubDate>
      <link>https://forem.com/arnabchat90/debugging-react-source-code-with-a-react-client-app-1l7</link>
      <guid>https://forem.com/arnabchat90/debugging-react-source-code-with-a-react-client-app-1l7</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwf8j73tme83mg0sli8zf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwf8j73tme83mg0sli8zf.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;br&gt;
If you are looking to contribute to react or for that matter any large open source project, it can be a daunting task to understand such huge code bases, making sense of how to build it and run it locally and develop against it and fix bugs.&lt;/p&gt;

&lt;p&gt;Today in this article we will tackle how to run the source code of &lt;a href="https://github.com/facebook/react" rel="noopener noreferrer"&gt;react&lt;/a&gt; locally, and then create a create-react-app client and create a &lt;strong&gt;symlink&lt;/strong&gt; between them, so that changing the source code of our local react package, can then be directly tested from the client app.&lt;/p&gt;

&lt;p&gt;This is not specific to react, but in general really useful to learn if you are building libraries and packages and you would like to test it with a real world client app.&lt;/p&gt;

&lt;p&gt;Before we start I would like to point you to the &lt;a href="https://reactjs.org/docs/how-to-contribute.html" rel="noopener noreferrer"&gt;How to Contribute&lt;/a&gt; official documentation from the react team.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up repos and symlink
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Step 1: 
Clone/Fork the react repository to your local file system  - &lt;code&gt;git clone https://github.com/facebook/react.git&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Step 2:
Once you have cloned the repo, run &lt;code&gt;npm install&lt;/code&gt; to install all its dependencies.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 3: &lt;br&gt;
React repository uses &lt;strong&gt;yarn workspaces&lt;/strong&gt;, which basically means, that they have divided the library into smaller re-usable packages, if you look at the &lt;strong&gt;package.json&lt;/strong&gt;, you can see that there is a &lt;strong&gt;workspaces property&lt;/strong&gt; which is an array, where they include everything inside &lt;strong&gt;packages folder&lt;/strong&gt;, where they have the actual packages like &lt;strong&gt;react&lt;/strong&gt; and &lt;strong&gt;react-dom&lt;/strong&gt; etc.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmo6e7iag1tuo0m3ztqyy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmo6e7iag1tuo0m3ztqyy.png" alt="react-packages"&gt;&lt;/a&gt;&lt;br&gt;
We will take a closer look at the react codebase later.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 4: &lt;strong&gt;Build&lt;/strong&gt; the react library - &lt;code&gt;yarn build react/index,react/jsx,react-dom/index,scheduler --type=NODE&lt;/code&gt;, this command is going to build the react, react-dom and schedular package, you can simply run the build script to build all packages, but for our purposes we need only react and react-dom.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 5 : Once the build command successfully runs, you will see a build folder inside the root of the project, you will see that inside the build folder the packages we built are created inside the node_modules folder, and you will see react and react dom.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fficqr3fz3nk2tr94ivja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fficqr3fz3nk2tr94ivja.png" alt="build-folder"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 6 : Create the &lt;strong&gt;symlinks&lt;/strong&gt;, cd into the build/node_modules/react package and then from there run the command - &lt;code&gt;yarn link&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdpyquxz9vzfg6l5nt9qi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdpyquxz9vzfg6l5nt9qi.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 7 : follow the same step for react-dom, cd into the react-dom folder and run yarn link.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting up the client app
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Step 1 : Use create-react-app or any other scaffolding tool, I am using cra because its quite popular and scaffold a client app - &lt;code&gt;npx create-react-app react-client&lt;/code&gt;. Do so in a different folder than where you cloned your react code.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Step 2: CRA runs yarn install for you, so if you run the app now, it will use the packages installed in your node modules, which has the react dependency from the online officially released version mentioned in your package json, but what we want is to use the built package we are currently working with. So here in the root directory of your client app run - &lt;code&gt;yarn link react react-dom&lt;/code&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwo4qo0mxa2nolxupxa7m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwo4qo0mxa2nolxupxa7m.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it you have now successfully created a symlink from you client app to the locally cloned library.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Step 3 - Start the client app using &lt;code&gt;yarn start&lt;/code&gt;, once the app runs open the debug window and checkout the path of the react library, it should point to your local build folder path and not the node_modules of your clients, and changes to your local library should reflect in the code your client app runs.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy50ju6j5urm5oiklo4d2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fy50ju6j5urm5oiklo4d2.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To give you some more details on how yarn/npm symlinks work refer to the image below - &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldq4izcoaovuptg5l7fu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fldq4izcoaovuptg5l7fu.png" alt="Alt Text"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this you are now ready to make changes to the react code, build it and directly use it with hot reload and other goodness in your cra client app.&lt;/p&gt;

&lt;p&gt;Hopefully this was helpful, feedback on the article would be appreciated.&lt;/p&gt;

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