Abstract Introduction to React Internals

I am a software developer from India. I am working in industry for last 7 years. I am interested in large scale distributed system, web and app development and compiler.
Search for a command to run...

I am a software developer from India. I am working in industry for last 7 years. I am interested in large scale distributed system, web and app development and compiler.
No comments yet. Be the first to comment.
In this series, I will collect my articles on JavaScript and JavaScript frameworks.
I have been doing front-end web development for last year only. Lately, I started playing with ReactJS. I have worked on vanilla JavaScript and jQuery for quite some time. My expert colleague at Amazon suggested I explore React. My only sin was I sai...
Fibonacci Series Problem LinkArticlesSolutions Climbing StairsGithub Fibonacci Series Min Cost Climbing Stairs Kadane’s Algorithm Problem LinkArticlesSolutions Maximum SubarrayGithub Maximum Sum Circular Subarray Maximum Product S...
Problem Link Summary Given an array nums of distinct integers, return all the possible permutations. You can return the answer in any order. Test cases: Input: nums = [1,2,3] Output: [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]] Input: nums = [0...
PaperNotes Kafka
Pattern 1: Fast and Slow Pointers Problem LinkArticlesSolution Linked List CycleGithub Happy NumberGithub Find the duplicate number Pattern 2: Sliding Window Problem LinkArticlesSolution Maximum Average SubarrayGithub Longest Subst...
After a long time, I started contributing to open-source software. For a long time, I had my eye on Deno. This week, I merged my first PR in Deno Lint. Following is the detailed post about the issue. Issue 425 Category: Refactoring Description of t...
React is a rich JavaScript framework. I think it's necessary to know a high-level internal working of a language or a framework to use it effectively. React's performance comes from its internal data structures and algorithms. In this article, I will not touch upon the actual codebase, data structures, or algorithms. I will share, on a high level, how React 16 renders and updates the UI. So, let's get started!
Smart data structures and dumb code works a lot better than the other way around. - Eric S. Raymond
During parsing, React creates an in-memory representation of components for easy rendering and updating. Typical React components can have multiple children and sibling components. We have multiple options to represent a component.
The following diagram shows the internal representation for the above component using both data structures.

For both the data structures,
One thing to note - I am taking a component for simplicity. I am not taking the entire page into the scope.
There can be two types of rendering - simple rendering without any updates or rendering page components after updates in a dynamic UI. Let's see how each scenario works.
In this case, React can use a combination of Depth-First and Breadth-First Traversal to render the entire component (for both types of representation).
In a dynamic user interface, we will have periodic updates as well as other high-priority activities like network calls. It is possible while rendering current changes, some other high-priority tasks come up. In that case, React may have to do a context switch. And that's where React shines. How does React handle that? Let's jump into that.
[I will use representation and DS (for data structure) inter-changeably.]
Let's call the original representation current copy. Let's go step by step.
Let's assume, for our example, Logo, About Us, and Products sub-components have changed after this operation. In that case, the following can be the effects list.

Why not update the UI while traversal? Let's suppose, the user creates an animation with tasks A and B. React is supposed to render A and B consecutively for creating a rich experience. Now, a high-priority task has arrived between A and B. Engine cannot ignore it. If the engine has to perform it between A and B by stopping B, the user will see a glitch between A and B. It is not desired user experience.
Why collect updates in a separate DS? As a high-priority task may arrive in between, rendering may not be as important as that task. In that case, React should switch context. After finishing that task, it can resume the traversal again. This collection of updates will help React to start the operation easily again.
After the collection of updates in a DS, it's time for applying the changes. It has to go in one shot. Otherwise, the user will see glitches and partial updates.
The above steps complete the entire update cycle. React engine applies various optimizations. We will explore React internals in detail in the next post. I promise I will touch on React data structures and codebase.
If you enjoyed the article, please don't forget to like it. In case you have any doubts, ask in the comment section.
Happy Learning! 😀