10. 重构 update props

34 阅读1分钟

本节课我们来重构一下之前的代码。

  • 首先我们可以把root改为wipRoot,这是正在构建中的根节点和下面的currentRoot区分。
  • update的调整,赋值给wipRoot
function update() {
    nextWorkOfUnit = { 
        dom: currentRoot.dom,
        props: currentRoot.props,
        alternate: currentRoot
    }
    wipRoot = nextWorkOfUnit
}
// 重构成
function update() {
    wipRoot = { 
        dom: currentRoot.dom,
        props: currentRoot.props,
        alternate: currentRoot
    }
    nextWorkOfUnit = wipRoot
}
​
function render(el, container) {
    nextWorkOfUnit = {
        dom: container,
        props: {
            children: [el]
        }
    }
    wipRoot = nextWorkOfUnit
}
​
// 重构成
function render(el, container) {
    wipRoot = {
        dom: container,
        props: {
            children: [el]
        }
    }
    nextWorkOfUnit = wipRoot
}
​
  • initChildren的重构,initChildren的职责改变了换一个名字更合适

    • 把initChildren改成reconcileChildren