What is reconciliation in React?
初級Reconciliation is the process React uses to determine how to efficiently update the real browser DOM when a component\'s state or props change. Instead of rebuilding the entire UI from scratch, React compares a new representation of the interface with the previous one and applies only the necessary changes.
How the Process Works
- Virtual DOM Creation: When an update is triggered, React creates a new Virtual DOM (a lightweight, in-memory copy of the UI).
- Diffing: React uses a "diffing algorithm" to compare the new Virtual DOM tree with the old one.
- Patching: Once the differences are identified, React calculates the minimal set of operations needed to update the actual DOM and applies them in a single batch to ensure high performance.
Key Rules of the Diffing Algorithm
To keep the reconciliation process fast (complexity), React follows two primary heuristic assumptions:
- Elements of Different Types: If the root elements of a subtree have different types (e.g., changing a
to a), React will tear down the old tree, destroy its state, and build the new one from scratch. - Elements of the Same Type: If elements are of the same type, React keeps the underlying DOM node and only updates the changed attributes or properties.
- Keys for Lists: For lists of elements, React uses the key prop to track which items have moved, been added, or removed, preventing unnecessary re-renders of stable items.
Evolution: React Fiber
Since React 16, the core reconciliation engine has been reimplemented as React Fiber. This modern architecture allows React to break rendering work into small units and pause, resume, or prioritize them, making complex UIs much more responsive by not blocking the main thread
↥ back to top