ZZomato·BehavioralL2Machine Coding

React Diffing Algorithm and Reconciliation

viaLeetCode

Question: Explain React's diffing algorithm and the reconciliation process — how does React decide what to re-render when state/props change?

Answer outline: React builds a virtual DOM tree on every render and diffs it against the previous virtual DOM tree using a heuristic O(n) algorithm (rather than the theoretical O(n^3) general tree-diff). Elements of different types are treated as entirely different subtrees (unmount old, mount new). Elements of the same type are compared prop-by-prop, updating only changed attributes. Lists use the key prop to match elements across renders so React can reorder/reuse existing DOM nodes instead of destroying and recreating them.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account