React Fiber, as we speak, is the most recently introduced reconciliation algorithm, which is nothing but the implementation of the previous algorithm which served till React version 15.
After the introduction of React 16, the community was brought to light by the much faster and more agile reconciler known as “Fiber”. We will be covering the following points which will provide us with a much deeper understanding of React Fiber.
The name “Fiber” was coined from React’s architecture and derived from ‘fiber’ which is a depiction of a node inside the DOM tree. Some of the reasons to justify why React Fiber was imperative include:
✔️ It gives us the ability to break down our work into various chunks.
✔️ It also provides the power to assign priority for every unit of work that demands completion.
✔️ It helps in providing a much-sharpened rendering of UI gestures/animations for user interactions.
Before diving much deeper into the technicalities of Fiber and understanding its true powers, let us first understand the old reconciler.
While learning React, the very first thing we learn is how the UI is rendered onto the screen for the first time or every time something is updated in the UI. Each time when we develop a new React application and the UI is rendered onto the screen for the first time, React builds a tree-like structure of nodes where each node is represented as a React element.
A clone of the rendered DOM tree is created by React(under the hood), known as the virtual DOM. Every time a component’s state or props gets updated, React makes a decision on updating the DOM tree by comparing the newly returned element with the previous one.
During the comparison, if the elements are found to be different in both the trees(previously rendered and newly created), then only the DOM is updated and the cumulative changes are passed on to the renderer. This complete process is referred to as “Reconciliation”.
The two primary components responsible for rendering element onto the DOM includes-
🔸 Reconciler: This component is primarily responsible for the computation part and for notifying React of the parts that need updating.
🔸 Renderer: This component takes the computed part and updates the rendered tree which ultimately is displayed on the user screen.
Let us understand how React converts the components into a tree structure of nodes by considering this basic example:
import { React, useState } from "react"; const App = () => { const [count, setCount] = useState(1); const handleClick = () => { setCount(count => { return count + 1; }); }; return ( <React.Fragment> <button onClick={handleClick}>Increment</button> <p>Count Value: {count}</p> </React.Fragment> ); }; export default App;
A number of activities are performed during the “reconciliation” process including the high-level operations performed by React during the first render along with managing the state change.
For instance, in the above example, some of the activities that React takes care of are:
🔹Updating the value of the count property in the App component
🔹 Handling props for the “p” element on every updation
🔹 Retrieval and comparison of the children of the App component
These activities collectively are cited as “work” in Fiber architecture. The type of work majorly depends on the React element type defined by the first parameter to the “createElement” function. This function is used inside the render method to create an element.
🅰️ The Component first goes through the JSX compiler to convert the element into React elements.
🅱️ The “React.createElement” function then creates the nodes in the following manner.
The previous algorithm came with certain limitations due to which Fiber reconciler had to be brought into action.
One such limitation was the immediate enforcement of any update that was made to the component due to the recursive nature of the algorithm. As the DOM tree gets larger and bigger in size, it becomes extremely difficult and costly to manage these updates and can ultimately lead to dropped frames.
Another issue that came with it was, that as Javascript is single-threaded in nature so all the tasks including UI updates, managing user actions, API calls, and handling animations and gestures were all managed by a single thread. When the first reconciliation phase is triggered, the current tree and updated tree(with all new updates) get rendered.
The reconciler detects the differences between the two trees within a single pass in a synchronous manner, thus preventing the main thread from taking care of other high-priority tasks.
To overcome these limitations, the Meta(Facebook) team came up with the new React Fiber Architecture enhanced and much smoother Fiber Reconciler in its React 16 update.
💠 Oversight Over Work Priority: As Fiber provides us with the feature of incremental rendering, it allows us to divide our work into smaller chunks and dispense it into multiple frames, allowing us to have complete control over the priority of work.
💠 Enhanced Performance: The sole purpose of introducing Fiber was to improve/enhance the performance of extremely large and complex React apps. Fiber provides React the ability to break the limits of the call stack which allows it to halt and resume rendering work when required.
💠 A Much Smoother Experience: React fiber promises a much smoother experience by letting React fine-tune the rendering part, responsible for ensuring that the most recurrent and paramount use cases are taken care of at the earliest.
Fiber’s architecture comprises two major phases, the first one being the “reconciliation” phase which is mostly referred to as the “render” phase in the React source code, and the second one which is referred to as the “commit” phase.
This phase is primarily responsible for walking through the tree of components and taking care of multiple activities including calling lifecycle hooks, alteration of props and state, retrieving “children” from components, and figuring out the DOM updates that need to be performed.
Basically, if React goes through the tree of components in a synchronous manner and performs some work on each of them, it may run over 16ms for an application code to execute the logic built which will ultimately cause the frames to drop resulting in stuttering visual effects.
In order to get around this issue, Fiber makes use of the “requestIdleCallback” function which is used to queue a function to be called during a browser’s idle period. Let us assume that if we put all the activities that will be performed on a component in a function like “executeTask” and use “requestIdleCallback” to schedule the work, our code snippet would look like this:
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); } });
Reacts performs the work on one component and then returns the reference to the next component that needs to be processed. Once the list is entirely computed, React schedules the changes that are to be executed in the next phase.
Of all the scheduled changes that come out of the reconciliation phase, React can choose to render a specific set of changes. After the commitment is done, React notifies the DOM to render the changes that were found during the reconciliation phase, and since all these changes are visible to the user, therefore this phase needs to be completed in a single call.
✔️ React Fiber allows us to handle runtime errors in a much cleaner fashion by setting up error boundaries which allow us to show an alternative screen in case something goes wrong.
✔️ It enhances the performance of our system by being able to split the limits of the call stack and allowing React to halt and resume rendering work when required.
✔️ It also provides us with support for returning some new render types including fragments and strings.
✔️ It allows us to create certain advanced elements using animations, gestures, layouts, and much more.
Through this blog, we understood how React Fiber reconciler makes things extra easy by splitting the work into several units, prioritizing each work, and also making it possible to pause, resume and abort any unit of work. In a fiber tree, it is the responsibility of the individual node to keep track of what is needed to make the above things possible.
How to Effectively Hire and Manage a Remote Team of Developers.
Mindbowser played a crucial role in helping us bring everything together into a unified, cohesive product. Their commitment to industry-standard coding practices made an enormous difference, allowing developers to seamlessly transition in and out of the project without any confusion....
CEO, MarketsAI
I'm thrilled to be partnering with Mindbowser on our journey with TravelRite. The collaboration has been exceptional, and I’m truly grateful for the dedication and expertise the team has brought to the development process. Their commitment to our mission is...
Founder & CEO, TravelRite
The Mindbowser team's professionalism consistently impressed me. Their commitment to quality shone through in every aspect of the project. They truly went the extra mile, ensuring they understood our needs perfectly and were always willing to invest the time to...
CTO, New Day Therapeutics
I collaborated with Mindbowser for several years on a complex SaaS platform project. They took over a partially completed project and successfully transformed it into a fully functional and robust platform. Throughout the entire process, the quality of their work...
President, E.B. Carlson
Mindbowser and team are professional, talented and very responsive. They got us through a challenging situation with our IOT product successfully. They will be our go to dev team going forward.
Founder, Cascada
Amazing team to work with. Very responsive and very skilled in both front and backend engineering. Looking forward to our next project together.
Co-Founder, Emerge
The team is great to work with. Very professional, on task, and efficient.
Founder, PeriopMD
I can not express enough how pleased we are with the whole team. From the first call and meeting, they took our vision and ran with it. Communication was easy and everyone was flexible to our schedule. I’m excited to...
Founder, Seeke
Mindbowser has truly been foundational in my journey from concept to design and onto that final launch phase.
CEO, KickSnap
We had very close go live timeline and Mindbowser team got us live a month before.
CEO, BuyNow WorldWide
If you want a team of great developers, I recommend them for the next project.
Founder, Teach Reach
Mindbowser built both iOS and Android apps for Mindworks, that have stood the test of time. 5 years later they still function quite beautifully. Their team always met their objectives and I'm very happy with the end result. Thank you!
Founder, Mindworks
Mindbowser has delivered a much better quality product than our previous tech vendors. Our product is stable and passed Well Architected Framework Review from AWS.
CEO, PurpleAnt
I am happy to share that we got USD 10k in cloud credits courtesy of our friends at Mindbowser. Thank you Pravin and Ayush, this means a lot to us.
CTO, Shortlist
Mindbowser is one of the reasons that our app is successful. These guys have been a great team.
Founder & CEO, MangoMirror
Kudos for all your hard work and diligence on the Telehealth platform project. You made it possible.
CEO, ThriveHealth
Mindbowser helped us build an awesome iOS app to bring balance to people’s lives.
CEO, SMILINGMIND
They were a very responsive team! Extremely easy to communicate and work with!
Founder & CEO, TotTech
We’ve had very little-to-no hiccups at all—it’s been a really pleasurable experience.
Co-Founder, TEAM8s
Mindbowser was very helpful with explaining the development process and started quickly on the project.
Executive Director of Product Development, Innovation Lab
The greatest benefit we got from Mindbowser is the expertise. Their team has developed apps in all different industries with all types of social proofs.
Co-Founder, Vesica
Mindbowser is professional, efficient and thorough.
Consultant, XPRIZE
Very committed, they create beautiful apps and are very benevolent. They have brilliant Ideas.
Founder, S.T.A.R.S of Wellness
Mindbowser was great; they listened to us a lot and helped us hone in on the actual idea of the app. They had put together fantastic wireframes for us.
Co-Founder, Flat Earth
Ayush was responsive and paired me with the best team member possible, to complete my complex vision and project. Could not be happier.
Founder, Child Life On Call
The team from Mindbowser stayed on task, asked the right questions, and completed the required tasks in a timely fashion! Strong work team!
CEO, SDOH2Health LLC
Mindbowser was easy to work with and hit the ground running, immediately feeling like part of our team.
CEO, Stealth Startup
Mindbowser was an excellent partner in developing my fitness app. They were patient, attentive, & understood my business needs. The end product exceeded my expectations. Thrilled to share it globally.
Owner, Phalanx
Mindbowser's expertise in tech, process & mobile development made them our choice for our app. The team was dedicated to the process & delivered high-quality features on time. They also gave valuable industry advice. Highly recommend them for app development...
Co-Founder, Fox&Fork