React源码解析8-元素第一次挂载

91 阅读2分钟

1.根元素的渲染和更新

image-20210507160914510

2.其他元素的挂载过程

1.当根元素执行performunitofpwork时候 里面所有子元素都会执行一边更新 这时候beginwork判断是什么类型的组件执行不同的更新

2.updateclasscomponent和updatehostRoot和updatehostComponen都会返回下一个需要挂载的元素给performunitofwork去调用 且都是在reconcilechildren之后创建啦fiber结构,第一层子fiber的结构就知道啦,然后依次下去对每个元素进行第一次构建

3.updatehostRoot和updatehostComponent的children是在props.children里的 updateclasscomponent是通过instance.render获取children的

因为React.createElement()这个函数就是会把里面的元素放到父元素的props.children上 所有当我们写jsx时候就是会有children能够获取到然后去recocile 所以class组件有render函数 root组件有Reactdom.render()的payload jsx有props.children

export function createElement(type, config, children) {
  let propName;

  // Reserved names are extracted
  const props = {};

  let key = null;
  let ref = null;
  let self = null;
  let source = null;
  //!看看有没有合理的ref和key
  if (config != null) {
    if (hasValidRef(config)) {
      ref = config.ref;
    }
    if (hasValidKey(config)) {
      key = '' + config.key;
    }

    self = config.__self === undefined ? null : config.__self;
    source = config.__source === undefined ? null : config.__source;
    // Remaining properties are added to a new props object
    //! 再看是不是内嵌的props key/ref/__self/__source 这些都不该出现在this.props里面 放到props这个新对象里面
    for (propName in config) {
      if (
        hasOwnProperty.call(config, propName) &&
        !RESERVED_PROPS.hasOwnProperty(propName)
      ) {
        props[propName] = config[propName];
      }
    }
  }

  // Children can be more than one argument, and those are transferred onto
  // the newly allocated props object.
  //!处理children 因为传入的参数可以多个除了前两个是type,config 后面都是children 剩下的参数转成children数组放到props上
  const childrenLength = arguments.length - 2;
  if (childrenLength === 1) {
    props.children = children;
  } else if (childrenLength > 1) {
    const childArray = Array(childrenLength);
    for (let i = 0; i < childrenLength; i++) {
      childArray[i] = arguments[i + 2];
    }
    if (__DEV__) {
      if (Object.freeze) {
        Object.freeze(childArray);
      }
    }
    props.children = childArray;
  }

  //!看看类组件有没有默认的props 也放到props对象来
  //class Cmp extends React.Component     Cmp.defaulrProps = {key:value} 相当于给类设置静态属性 有则不设置 没有则设置
  if (type && type.defaultProps) {
    const defaultProps = type.defaultProps;
    for (propName in defaultProps) {
      if (props[propName] === undefined) {
        props[propName] = defaultProps[propName];
      }
    }
  }
  //!Dev不管
  if (__DEV__) {
    if (key || ref) {
      const displayName =
        typeof type === 'function'
          ? type.displayName || type.name || 'Unknown'
          : type;
      if (key) {
        defineKeyPropWarningGetter(props, displayName);
      }
      if (ref) {
        defineRefPropWarningGetter(props, displayName);
      }
    }
  }
  //!返回一个React元素
  return ReactElement(
    type,
    key,
    ref,
    self,
    source,
    ReactCurrentOwner.current,
    props,
  );
}

4.比如class就是调用instance的render方法获取子元素

总结:reactdom.render获取总的element 开启workloop 里面子元素获取的children 然后调用recocilechildren创建fiber树 获取的子元素返回给当作下一个的unitwork直到建立一个fiber树