源码笔记-ReactElement

123 阅读1分钟

源码笔记-ReactElement

react使用bebal将jsx语法转换成js语法,会经常使用React.createElement()进行创建
在ReactElement.js文件中有createElement()方法。
该方法有三个参数:type, config, children

  • type:指的是ReactElement的类型
    字符串:div、p这些代表原生DOM,称为HostComponent
    class类型:继承Component或者PureComponent的组件,称为ClassComponent
    方法:function Component
    原生提供的Fragment、AsycMode等是Symbol,会被特殊处理
  • config:就是属性
  • children:内容或是子标签

例如:

function Comp(){
    	return <a>123</a>
    }
    <Comp id="div" key="key">
    	<span>1</span>
        <span>2</span> 
    </Comp>
    转换成js语法
    function Comp(){
    	return React.creatElement("a", null, "123")
    }
    React.createElement(Comp, 
    {
    	id: "div",
        key: "key"
    },
    React.createElement("span", null, "1"),
    React.createElement("span", null, "2")
    )

在RractElement看到的变量$$typeof,这是一个 REACT_ELEMENT_TYPE常量

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;

  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
    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.
  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;
  }

  // Resolve default props
  if (type && type.defaultProps) {
    const defaultProps = type.defaultProps;
    for (propName in defaultProps) {
      if (props[propName] === undefined) {
        props[propName] = defaultProps[propName];
      }
    }
  }
  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);
      }
    }
  }
  return ReactElement(
    type,
    key,
    ref,
    self,
    source,
    ReactCurrentOwner.current,
    props,
  );
}

const ReactElement = function(type, key, ref, self, source, owner, props) {
  const element = {
    $$typeof: REACT_ELEMENT_TYPE,
    type: type,
    key: key,
    ref: ref,
    props: props,
    _owner: owner,
  };

  if (__DEV__) {
    Object.defineProperty(element._store, 'validated', {
      configurable: false,
      enumerable: false,
      writable: true,
      value: false,
    });
    Object.defineProperty(element, '_self', {
      configurable: false,
      enumerable: false,
      writable: false,
      value: self,
    });
    Object.defineProperty(element, '_source', {
      configurable: false,
      enumerable: false,
      writable: false,
      value: source,
    });
    if (Object.freeze) {
      Object.freeze(element.props);
      Object.freeze(element);
    }
  }

  return element;
};

ReactElement只是一个用来承载信息的容器,他会告诉后续的操作这个节点的以下信息:
type类型,用于判断如何创建节点
key和ref这些特殊信息
props新的属性内容