Understand React Fiber in a simplified way
As we all know ReactJs is getting too much popularity because of its simplicity and performance. There are lots of factors for its performance but its new reconciliation algorithm “React Fiber” is playing a very crucial role.
So in this blog, I will try to explain how react fiber works under the hood.
What is React Fiber?
React Fiber?is the new?reconciliation algorithm. Reconciliation is the process of comparing or diffing old trees with a new tree in order to find what is changed or modified.
Why introduced react fiber?
React 16.0 was released with an update to the React core algorithm. This new core architecture is named “Fiber.” Facebook has completely rewritten the internals of React from the ground up while keeping the public API essentially unchanged; in simple terms, it means only changing the engine of a running car.
With this release, some new features are added, like Asynchronous Rendering, Portals, Error Boundaries, and Fragments (i.e. return array of elements). Incremental rendering is the headline addition to React, which adds the ability to split rendering work into chunks.
Also, in the original reconciliation algorithm (now called Stack Reconciler), the processing of component trees was done synchronously in a single pass, so the main thread was not available for other UI-related tasks like animation, layouts, and gesture handling.
How does react fiber work?
A?fiber?(not Fiber with a capital ‘F’) is a JavaScript object that contains information about a component, its input, and output. At any time, a component instance has at most two fibers that correspond to it: the current fiber and the work-in-progress fiber. A fiber can be defined as a unit of work.
领英推荐
Fiber prioritizes work on 7 levels:
React Fiber performs reconciliation in two phases: Render and Commit
1. Lifecycle methods called during the render phase:
2. Lifecycle methods called during the commit phase:
The earlier whole reconciliation process was synchronous (recursive), but in Fiber, it is divided into two phases. The render phase (a.k.a. Reconciliation phase) is asynchronous, so three of the lifecycle methods were marked unsafe because putting the code with side-effects inside these methods can cause problems, as lifecycle methods of different components are not guaranteed to fire in a predictable order.
React Fiber uses?requestIdleCallback()?to schedule the low priority work and?requestAnimationFrame()?to schedule high-priority work.
Problems with Current Implementation:
So in a nutshell, what makes Fiber interesting?
Fiber is currently available for use but it runs in compatibility mode with the current implementation.
Thanks for reading this article and I hope it enhances your understanding of the react fiber ????.