Understanding React Fiber: The Architecture Behind React 16+

These days, the most current reconciliation technique to be presented is called React Fibre. It is nothing more than the previous algorithm's implementation, which was used up to React version 15.
Table of Contents

Need Help? The Cybernative Team Is Here To Answer Your Queries.

Table of Contents

The community gained visibility with the release of React 16 thanks to “Fibre,” a reconciler that is significantly quicker and more flexible. With a much deeper grasp of React Fibre, we will be going over the following aspects.


Understanding React Fiber

Fibre, which represents a node inside the DOM tree, is the root of the term “Fibre,” which was inspired by React’s design. The following are a few arguments in favour of the necessity of React Fibre:

  • It provides the capacity to prioritise each unit of work that has to be completed in addition to enabling us to divide our job into different pieces.
  • It gives UI gestures and animations for user interactions a significantly sharper portrayal.

Let us examine the ancient reconciler first before delving much farther into the specifics of Fibre and realising its potential. 


What is the algorithm for React Reconciliation and how does it operate?

First things first: when studying React, we are taught how the user interface is rendered on screen when it is first created or changed. Every time a ReactJS development company develops a new app is developed and the user interface is initially displayed on the screen, React creates a network of nodes like a tree, with each node being represented by a React element.


React creates what is known as the virtual DOM, which is essentially a copy of the displayed DOM tree. React decides whether to update the DOM tree for each component when its state or props are modified by contrasting the recently returned element with the one that came before it. 


React creates what is known as the virtual DOM, which is essentially a copy of the displayed DOM tree. React decides whether to update the DOM tree for each component when its state or props are modified by contrasting the recently returned element with the one that came before it. 


The renderer receives the cumulative modifications only after the DOM has been updated during the comparison process if it is discovered that the items in the two trees—the freshly constructed and the previously rendered ones—are different. We call this whole procedure “Reconciliation.” The rendering of an element onto the DOM is accomplished by two main components, namely:


Reconciler: The main duties of this component are calculation and informing React of the sections that require updates.


Renderer: This phase changes the rendered tree, which is eventually shown on the user’s screen, using the calculated portion as input.


Taking a look at this simple example will help us understand how React transforms the components into a node tree:

import { React, useState } from “react”;

const App = () => {

const [count, setCount] = useState(1);

consthandleClick = () => {

setCount(count => {

return count + 1;

});

};

return (

<React.Fragment>

<button onClick={handleClick}>Increment</button>

<p>Count Value: {count}</p>

</React.Fragment>

);

};

export default App;

The high-level actions carried out by React during the first render as well as handling the state change are among the several tasks carried out throughout the “reconciliation” phase


In the example above, for instance, React handles the following tasks:

The children of the App component are retrieved and compared. The value of the count property in the App component is updated. Props for the “p” element are handled on each update.


Fibre architecture refers to these actions taken as a whole as “work.” React element type, as given by the first parameter to the “createElement” method, largely determines the type of work. An element is created using this function within the render method.


What made new architecture necessary?

The implementation of Fibre reconciler was necessary since the prior approach had various drawbacks. One restriction stemming from the recursive structure of the method was the prompt implementation of any modifications made to the component.


The implementation of Fibre reconciler was necessary since the prior approach had various drawbacks. One restriction stemming from the recursive structure of the method was the prompt implementation of any modifications made to the component.


Eventually, lost frames may result from managing these changes, which becomes more expensive and challenging as the DOM tree grows in size.


Because Javascript is a single-threaded language, all of the duties associated with it, such as UI updates, user action management, API calls, and animation and gesture handling, were handled by a single thread.


Because Javascript is a single-threaded language, all of the duties associated with it, such as UI updates, user action management, API calls, and animation and gesture handling, were handled by a single thread.


This presented another problem. The current tree and the updated tree (with all new changes) are shown when the first reconciliation phase begins.


The reconciler stops the main thread from doing other high-priority operations by synchronously detecting the discrepancies between the two trees in a single pass. In its React 16 version, the Meta (Facebook) team developed a new, improved, and more smoother Fibre Reconciler to get around these restrictions. 


The Reason for Introducing React Fibre

Prioritising Oversight Above Work

We are able to break our work into smaller portions and distribute it across numerous frames thanks to Fiber’s incremental rendering functionality, which gives us total control over the order of importance of our tasks.


Prioritising Oversight Above Work

Large scale enterprise applications can be decomposed into smaller module with independent features. Each independent micro service can handle specific business operational tasks.


Increased Efficiency:

React apps that are incredibly sophisticated and enormous in size were the only reason Fibre was introduced. When necessary, Fibre enables React to pause and restart rendering activity by breaking the boundaries of the call stack. 


A Much More Easy Experience:

Allowing React to optimise the rendering process which is in charge of making sure that the most important and often occurring use cases are handled first React fibre ensures a significantly more seamless user experience.


The Fibre Architecture and Its Component Phases

The architecture of Fibre is divided into two main stages: the “commit” phase, sometimes known as the “reconciliation” phase, and the “render” phase, which is commonly referred to as such in the React source code.


Phase 1: Render

In this phase, you will mostly find yourself traversing the component tree and handling various tasks including invoking lifecycle hooks, changing props and state, getting “children” out of components, and determining which DOM updates require attention.


React can generate stuttering visual effects if it traverses the component tree synchronously and works on each component. This can take longer than 16 milliseconds for the application code to execute the logic, which in turn causes the frames to drop.


Fibre uses a function named “requestIdleCallback” to queue up a call to be made during a browser’s idle time in order to circumvent this problem. Assuming that we schedule the work using “requestIdleCallback” and place all of the tasks that need to be completed on a component in a function like “executeTask,” our code snippet would become as follows:

requestIdleCallback(deadline => {
// While time is remaining, perform work for other part of the components tree
while (
(deadline.timeRemaining() > 0 || deadline.didTimeout) &&
nextComponent
) {
nextComponent = performWork(nextComponent);
}
});

When Reacts completes processing one component, it returns the reference to the subsequent component that needs to be handled. Following the completion of the list’s computation, React plans the modifications that will be carried out in the following stage.


Phase 2: Commit

React offers the option to render one or more of the planned modifications that result from the reconciliation step. React next tells the DOM to render the changes discovered during the reconciliation phase. Since all of these changes are visible to the user, this phase must be finished in a single call once the commitment is finished.


React Fiber’s primary advantages.

  • React Fibre lets us use error boundaries to display a different screen in the event of a problem, allowing us to manage runtime problems in a more cleaner manner.
  • It allows React to stop and start rendering as needed, which improves system performance by dividing the call stack constraints.
  • It also gives us the ability to receive certain additional render types, such strings and fragments.
  • It enables us to use layouts, motions, animations, and much more to construct some sophisticated pieces. 

Final Consideration

By breaking up the work into many parts, giving each job a priority, and enabling us to pause, continue, and stop any unit of work, the React Fibre reconciler simplifies things even more, as this blog explained. It is each node’s duty to maintain track of the resources required to enable the aforementioned functions in a fibre tree.


Related Posts:

For more updates and information about ReactJS Development, follow us on LinkedIN.

Need Help? The Cybernative Team Is Here To Answer Your Queries.