Loading...
Loading...
Virtual DOM is a lightweight copy of the real DOM stored in RAM.
React updates this copy instead of directly modifying the real DOM to avoid triggering unnecessary "expensive" operations (layout, painting, reflow) in the browser.
When all changes are made, React compares old and new versions of Virtual DOM (diffing) and precisely updates the real DOM (reconciliation).
React applies an efficient algorithm with two key assumptions:
Different element types → different trees
key value for children
key) allow React to understand which elements persisted, which were added and which were removed.Virtual DOM Update
React captures changes (e.g., setState or useState call).
Diffing
React compares previous Virtual DOM with new one, identifying differences.
Reconciliation
Important nuance:
If parent component renders, by default its child components also re-render unless additional optimizations are applied (React.memo, shouldComponentUpdate, etc.)