An introduction to React Fiber (the algorithm behind React) React Fiber is the core reconciliation algorithm behind React. It was introduced in React 16. The old reconciler algorithm was called Stack Reconciler. The new Fiber architecture aims to provide the following benefits: -> Incremental rendering for better performance -> Smooth animations -> Responsiveness to user actions It allows to divide rendering work into chunks and prioritize updates. Some other features included returning multiple elements, better error handling, and portals. So, what is a fiber? A fiber is a simple Javascript object. It represents the React element or node of the Virtual DOM tree. As React renders the App for the first time, it goes through each node and creates a fiber node. Each fiber is connected to its parent, sibling, and child thus forming a linked list. In simple terms, we call it a tree of fibers or fiber tree. Now how does React Fiber work? When the App is first mounted, Fiber constructs a tree of components called current. The current tree is responsible for rendering the UI. Whenever there is an update or state change, Fiber constructs a tree in parallel called workInProgress. Fiber tree traversal happens like this: -> Start: Fiber starts traversal from the topmost React element and creates a fiber node for it. -> Child: Then, it goes to the child element and creates a fiber node for this element. This continues until the leaf element is reached. -> Sibling: Now, it checks for the sibling element if there is any. If there is any sibling, it traverses the sibling subtree until the leaf element of the sibling. -> Return: If there is no sibling, then it returns to the parent. React performs work on this workInProgress tree. The workInProgress tree now has updated elements in response to the update. In the commit phase, React switches the current tree with the updated workInProgress tree. This switch renders the new updates to the UI. This is how React Fiber works behind the scenes to update the UI and optimize React performance. This is a high-level explanation of how React Fiber algorithm works. Let me know in the comments: Would you like to go deeper into React algorithms and React internals? If you are into React development: Follow for more similar content on React performance and frontend optimization #React #ReactFiber #ReactJS #FrontendDevelopment #WebDevelopment #JavaScript #ReactPerformance #ReactInternals #ReactAlgorithm #ReactReconciliation #React16 #VirtualDOM #FrontendEngineer #MERNStack #NextJS #WebDevCommunity #LearnReact #ReactDevelopers #CodingTips #DevCommunity
Yaser Kazi Thanks for reposting
Thanks for reposting Loi Dong
Mayank Kumar Thanks for reposting