Skip to content

虚拟 DOM

一句话解释

虚拟 DOM 是 lightweight in-memory representation 的真实 DOM 那个 React uses 到 calculate minimal 设置的 更改之前 touching 浏览器.

用大白话说

The Virtual DOM (VDOM) is a JavaScript object tree that mirrors the real DOM. When your component's state or props change, React builds a new virtual DOM tree from the updated JSX. It then compares this new tree to the previous one — a process called diffing or reconciliation — and computes the exact set of DOM mutations needed.

这个is faster than directly manipulating the real DOM because reading and writing the real DOM is slow (it triggers layout recalculations). React batches the computed changes and applies them all at once, minimizing browser reflows.

You rarely interact with the Virtual DOM directly. It is an implementation detail that makes React's declarative model practical: you describe the desired UI, and React figures out the most efficient way to update the real DOM to match.

示例

jsx
// When state changes, React creates a new VDOM tree:
// Old:  <div><h1>Hello</h1></div>
// New:  <div><h1>World</h1></div>
// React diffs the two trees and only updates the text node,
// not the entire <div> or <h1>.

React compares 旧 and 新虚拟 DOM 树, 查找那个仅 文本内容更改, and 更新只是那个 one 文本节点 in 真实 DOM.

常见混淆

  • 容易混淆的对象: 真实 DOM

    关键区别: 真实 DOM 是浏览器's live, heavy 对象 model. 虚拟 DOM 是 lightweight JS 复制 React maintains in memory. React 更新真实 DOM 仅之后 diffing 虚拟 one.

相关术语

相关资源

相关课程

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误

← 返回术语表