Skip to the content.

react源码阅读笔记:二、关于 React.createElement

函数定义

const ReactElement = function(type, key, ref, self, source, owner, props) { ... }
  1. ReactElement是个啥
  /**
    * Factory method to create a new React element. This no longer adheres to
    * the class pattern, so do not use new to call it. Also, instanceof check
    * will not work. Instead test $$typeof field against Symbol.for('react.element') to check
    * if something is a React Element.
    *
    **/

根据注释说明:ReactElement是一个创建React元素的工厂方法,简言之就是React类型的元素对象就是ReactElement,再具体一点的东西到后面再进行分析。

  1. 关于ReactElement方法的参数 type, key, ref, owner, props不多说
    1. self 用来检测thisowner的不同,用在dev环境里面,调用React.createElement的时候从config中取出来的。
    2. source 用来标注文件名,行号和其它信息的参数,也只用在dev环境里面
  2. 关于此函数的返回值 ReactElement的返回值是一个如下的对象
      const element = {
     // This tag allows us to uniquely identify this as a React Element
     $$typeof: REACT_ELEMENT_TYPE,
    
     // Built-in properties that belong on the element
     type: type,
     key: key,
     ref: ref,
     props: props,
    
     // Record the component responsible for creating this element.
     _owner: owner,
      };
    

    $$typeof用来标识此对象是否是一个ReactElement元素