Understanding how React updates the UI starts with the difference between the DOM and the Virtual DOM. These concepts explain how React calculates updates and applies them efficiently.
Here are 4 concepts every React developer should understand.
DOM (Document Object Model)
The DOM is a tree-like object representation of an HTML document that the browser uses to expose and manipulate page content.
<div><h1>Hello</h1></div>
The browser represents this structure as nodes that JavaScript can access and modify.
Key points:
Virtual DOM
The Virtual DOM is a common term for React’s in-memory representation of UI elements. React uses this representation to determine what needs to change in the actual DOM.
function App() {return (<div><h1>Hello</h1></div>);}
React creates element descriptions from JSX and uses reconciliation to determine the necessary updates.
Key points:
When state changes, React typically follows this process:
import { useState } from "react";function Counter() {const [count, setCount] = useState(0);return (<div><h1>Count: {count}</h1><button onClick={() => setCount(count + 1)}>Increment</button></div>);}
When the user clicks the button, React recalculates the component’s UI and updates the relevant DOM content.
Important: React does not necessarily recreate the entire Virtual DOM tree from scratch, nor does it always update only one node. The actual work depends on the component tree and reconciliation results.
React uses reconciliation to determine which DOM changes are necessary.
For example, if a counter changes from 0 to 1, React can update the text content without recreating the surrounding elements.
This helps avoid unnecessary DOM mutations.
However, performance also depends on component rendering, JavaScript execution, layout, painting, and the complexity of the UI.
Not necessarily. Creating and comparing JavaScript objects also has a cost.
React’s advantage is its declarative programming model and its ability to calculate and coordinate UI updates efficiently. Direct DOM manipulation can be faster for certain simple, specialized operations.
React also batches updates in appropriate situations, reducing redundant work.