是什么
根據(jù)真實DOM創(chuàng)建的映射的數(shù)據(jù)結(jié)構(gòu)扫沼,
要區(qū)分出兩個的不同辱士, 就是diff算法
有什么用
如何實現(xiàn)
大概思路:
- 用JavaScript對象結(jié)構(gòu)表示DOM樹的結(jié)構(gòu);然后用這個樹構(gòu)建真正的DOM樹茴晋,放到頁面中呛每;
- 狀態(tài)變更時,重新構(gòu)造一棵樹乓诽,將新樹和舊樹進行比較帜羊,記錄差異;
- 將2中的差異應用到1構(gòu)建的真實DOM樹上鸠天,更新視圖讼育。
代碼實現(xiàn)
class VNode {
constructor(tag, children, text) {
this.tag = tag
this.text = text
this.children = children
}
render() {
if(this.tag === '#text') {
return document.createTextNode(this.text)
}
let el = document.createElement(this.tag)
this.children.forEach(vChild => {
el.appendChild(vChild.render())
})
return el
}
}
function v(tag, children, text) {
if(typeof children === 'string') {
text = children
children = []
}
return new VNode(tag, children, text)
}
let vNodes = v('div', [
v('p', [
v('span', [ v('#text', 'xiedaimala.com') ] )
]
),
v('span', [
v('#text', 'jirengu.com')
])
]
)
console.log(vNodes)
console.log(vNodes.render())
function patchElement(parent, newVNode, oldVNode, index = 0) {
if(!oldVNode) {
parent.appendChild(newVNode.render())
} else if(!newVNode) {
parent.removeChild(parent.childNodes[index])
} else if(newVNode.tag !== oldVNode.tag || newVNode.text !== oldVNode.text) {
parent.replaceChild(newVNode.render(), parent.childNodes[index])
} else {
for(let i = 0; i < newVNode.children.length || i < oldVNode.children.length; i++) {
patchElement(parent.childNodes[index], newVNode.children[i], oldVNode.children[i], i)
}
}
}
let vNodes1 = v('div', [
v('p', [
v('span', [ v('#text', 'xiedaimala.com') ] )
]
),
v('span', [
v('#text', 'jirengu.com')
])
]
)
let vNodes2 = v('div', [
v('p', [
v('span', [
v('#text', 'xiedaimala.com')
] )
]
),
v('span', [
v('#text', 'jirengu.coms'),
v('#text', 'ruoyu')
])
]
)
const root = document.querySelector('#root')
patchElement(root, vNodes1)