
如果您听说过 react 或 vue 等前端库,您可能遇到过术语 虚拟 dom。虚拟 dom 是一个聪明的概念,它可以通过提高 dom 更新效率来帮助加快 web 开发速度。
在本指南中,我们将详细介绍如何使用通用的类似代码的步骤从头开始实现简单的虚拟 dom。
虚拟 dom 只是真实 dom(网页结构)的轻量级内存表示。我们不是直接更新真实 dom(这很慢),而是首先对虚拟 dom 进行更改,弄清楚发生了什么变化,然后只更新真实 dom 中需要更新的部分。这可以节省时间并使您的应用程序运行得更快!
将网页的结构想象成一棵树,其中每个元素(如 <div>、<p> 或 <h1>)都是树中的一个“节点”。 虚拟 dom 节点 是一个代表这些元素之一的小对象。
这是一个例子:
virtual dom node:
{
type: 'div',
props: { id: 'container' }, // attributes like id, class, etc.
children: [ // children inside this element
{
type: 'p', // a <p> tag (paragraph)
props: {},
children: ['hello, world!'] // text inside the <p> tag
}
]
}
这描述了一个 id="container" 的 <div> 元素,其中包含一个带有文本 “hello, world!”.
的 <p> 元素。现在我们有了虚拟 dom,我们需要一种方法将其转换为页面上的真实 html。
让我们编写一个名为 render 的函数,它接收虚拟 dom 节点并将其转换为实际的 html 元素。
function render(vnode) {
// 1. create a real element based on the virtual dom type (e.g., div, p).
const element = document.createelement(vnode.type);
// 2. apply any attributes (props) like id, class, etc.
for (const [key, value] of object.entries(vnode.props)) {
element.setattribute(key, value);
}
// 3. process the children of this virtual dom node.
vnode.children.foreach(child => {
if (typeof child === 'string') {
// if the child is just text, create a text node.
element.appendchild(document.createtextnode(child));
} else {
// if the child is another virtual dom node, recursively render it.
element.appendchild(render(child));
}
});
return element; // return the real dom element.
}
当我们的网络应用程序发生某些变化(例如文本或元素的样式)时,我们会创建一个新的虚拟 dom。但在更新真实 dom 之前,我们需要比较旧 virtual dom 和 新 virtual dom 来找出发生了什么变化。这称为“比较”。
让我们创建一个比较两个虚拟 dom 的函数:
function diff(oldvnode, newvnode) {
// if the node type has changed (e.g., from 'div' to 'p'), replace the node.
if (oldvnode.type !== newvnode.type) {
return { type: 'replace', newvnode };
}
// if the text inside has changed, we need to update the text.
if (typeof oldvnode === 'string' && oldvnode !== newvnode) {
return { type: 'text', newvnode };
}
// compare the properties (like id, class, etc.).
const proppatches = diffprops(oldvnode.props, newvnode.props);
// compare the children.
const childpatches = diffchildren(oldvnode.children, newvnode.children);
return { type: 'update', proppatches, childpatches };
}
一旦我们知道发生了什么变化,我们就需要将这些更改应用到真实的 dom 上。我们将此过程称为修补。
修补功能的外观如下:
function patch(parent, patches, index = 0) {
if (!patches) return; // If there are no changes, do nothing.
const element = parent.childNodes[index]; // The element we’re patching.
switch (patches.type) {
case 'REPLACE':
// Replace the old element with a new one.
parent.replaceChild(render(patches.newVNode), element);
break;
case 'TEXT':
// Update the text content of the element.
element.textContent = patches.newVNode;
break;
case 'UPDATE':
// Update the element’s properties and children.
patchProps(element, patches.propPatches);
patches.childPatches.forEach((childPatch, i) => patch(element, childPatch, i));
break;
}
}
虚拟 dom 是一个强大的工具,通过减少对真实 dom 的不必要的更改,可以更快地更新用户界面。通过实现虚拟 dom,我们可以优化 web 应用程序更新和渲染元素的方式,从而带来更快、更流畅的用户体验。
这是虚拟 dom 概念的基本实现,但您现在已经有了理解 react 等框架如何使用它的基础!
以上就是从头开始设计虚拟 DOM:分步指南的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号