Skip to content

functionComponent

Anshi edited this page Apr 19, 2019 · 1 revision

函数组件

函数组件的实现较为简单,几乎就是在几个 switch 语句里添加几个 case 的逻辑。

beginWork - 函数组件的子元素

首先是对于 workInProgress 的处理,针对函数组件,没有实例,newChildElements 的计算就是 result = f(props),函数式的思维再一次展现的淋漓尽致(???),

function updateFunctionComponent(wipFiber: Fiber):void {
  let newChildElements = wipFiber.type(wipFiber.pendingProps);
  reconcileChildrenArray(wipFiber, newChildElements);
}

createChild - 函数组件 fiber

在创建函数节点的 fiber 时,需要根据 element 的 type 得出 fiber 的 tag,这里需要区别的是类组件,二者的 type 其实都是函数,如果用原型链来区分,那未免有些繁琐,在观看 react Component 的一些源码后,发现存在一个Component.prototype.isReactComponent的属性,所以我也引入了这个属性,并以此区分 函数组件和类组件。

    case "function":
      if (ele.type.isReactComponent) {
        tag = ClassComponent;
      } else {
        tag = FunctionComponent;
      }
      break;

Clone this wiki locally