TO understand the Virtual Dom.
Doucmention of vitural DOM
vitrual dom reffernces link
Virtual DOM, Fiber, and Reconciliation
In modern JavaScript frameworks, such as React, concepts like the Virtual DOM, Fiber, and Reconciliation are essential for optimizing updates to the user interface. Let's break down each of these terms
1. Virtual DOM
The Virtual DOM (VDOM) is a lightweight copy or abstraction of the actual DOM (Document Object Model). Instead of directly manipulating the real DOM, which can be slow and inefficient, React uses a virtual DOM to track changes and efficiently update the UI. Here’s how it works:
Representation: The VDOM represents the UI as a tree of JavaScript objects.
Efficiency: Rather than re-rendering the entire UI, React updates only parts of the virtual DOM that have changed.
Rendering Flow:
The initial state of the application is rendered into the Virtual DOM.
When the state or props change, a new Virtual DOM is created.
The new Virtual DOM is compared to the previous one using a diffing algorithm (Reconciliation).
React then updates only the parts of the real DOM that have changed, making the process faster and more efficient.
2. Reconciliation
Reconciliation is the process by which React updates the actual DOM to match the changes in the virtual DOM. The goal of reconciliation is to minimize the number of changes made to the real DOM in order to optimize performance. React uses an efficient diffing algorithm during this process.
Diffing Algorithm: React compares the new virtual DOM tree with the previous one to identify what has changed. It then calculates the minimum number of operations needed to update the real DOM.
Key concepts:
Elements: React elements are immutable. When you update the state, React creates new elements.
Keys: When rendering lists, React uses a
key
attribute to identify elements uniquely. This allows React to quickly identify which items have changed, been added, or removed.
3. Fiber
Fiber is the new reconciliation algorithm introduced in React 16, designed to improve the performance and flexibility of React's rendering process. It significantly enhanced how React handles updates, especially in cases with complex UIs or animations.
What Fiber changes: Prior to React 16, React used a synchronous rendering algorithm that blocked the main thread and made the UI less responsive. Fiber breaks down the rendering process into smaller units of work, making it asynchronous and enabling features like interruptible rendering.
Key features of Fiber:
Incremental Rendering: Instead of completing a whole render in one go, Fiber breaks the render into chunks that can be executed over time, allowing the UI to remain responsive.
Prioritization: Fiber allows React to prioritize updates. For instance, animations or urgent updates can be processed first, and less critical updates (e.g., background tasks) can be deferred.
Interruptibility: If React is in the middle of a render, it can pause and do other tasks (such as handling user input) before continuing.
Fiber Architecture:
Fiber Nodes: Each node in the tree is a "fiber." Each fiber represents a unit of work and has a reference to its child, parent, and sibling fibers.
Work Loops: Fiber uses work loops to handle different stages of the rendering process, including preparation, commit, and update phases. This allows React to split tasks into smaller pieces and schedule them efficiently.
How They Work Together
Initial Render:
React first creates the virtual DOM, which represents the UI based on the current state and props.
The Virtual DOM is then compared to the real DOM, and the necessary updates are made (this is the first stage of reconciliation).
State Change:
When the state or props of a component change, React creates a new virtual DOM.
React uses the reconciliation process (via the diffing algorithm) to compare the new virtual DOM with the old one.
Fiber comes into play by breaking down the rendering process into smaller, manageable tasks that can be scheduled over time.
Update and Commit:
After the diffing process, React determines what changes need to be made to the real DOM.
React commits the changes by directly manipulating the real DOM in a minimal and efficient way.
Benefits of These Concepts
Performance: By using a Virtual DOM and efficient reconciliation (with Fiber), React minimizes the number of changes made to the real DOM, significantly improving performance, especially on complex web apps.
User Experience: With the Fiber architecture, React can prioritize high-priority updates (like animations and user inputs) while deferring less critical ones, ensuring the app remains responsive.
Flexibility: Fiber introduces a more flexible system, allowing React to handle complex updates more efficiently, including handling errors, interrupting tasks, and optimizing the rendering process for various devices and network conditions.
Summary
Virtual DOM: An abstraction of the real DOM to optimize rendering performance.
Reconciliation: The process of comparing the Virtual DOM with the real DOM to identify and update only the changed parts.
Fiber: The new, more efficient reconciliation algorithm in React 16+ that breaks the rendering process into smaller tasks, allowing for asynchronous and prioritized updates.