React.createElement(type, props, children);

    1. function isValidElementType(type) {
    2. return typeof type === 'string' ||
    3. typeof type === 'function' ||
    4. type === REACT_FRAGMENT_TYPE || // Symbol(react.fragment)
    5. type === REACT_CONCURRENT_MODE_TYPE || // Symbol(react.concurrent_mode)
    6. type === REACT_PROFILER_TYPE || // Symbol(react.profiler)
    7. type === REACT_STRICT_MODE_TYPE || // Symbol(react.strict_mode)
    8. type === REACT_SUSPENSE_TYPE || // Symbol(react.suspense)
    9. type === REACT_SUSPENSE_LIST_TYPE || // Symbol(react.suspense_list)
    10. typeof type === 'object' &&
    11. type !== null &&
    12. (type.$$typeof === REACT_LAZY_TYPE || // Symbol(react.lazy)
    13. type.$$typeof === REACT_MEMO_TYPE || // Symbol(react.memo)
    14. type.$$typeof === REACT_PROVIDER_TYPE || // Symbol(react.provider)
    15. type.$$typeof === REACT_CONTEXT_TYPE || // Symbol(react.context)
    16. type.$$typeof === REACT_FORWARD_REF_TYPE || // Symbol(react.forward_ref)
    17. type.$$typeof === REACT_FUNDAMENTAL_TYPE || // Symbol(react.fundamental)
    18. type.$$typeof === REACT_RESPONDER_TYPE); // Symbol(react.responder)
    19. }
    1. function createElementWithValidation(type, props, children) {
    2. var validType = isValidElementType(type);
    3. if (!validType) {
    4. var info = '';
    5. if (type === undefined || typeof type === 'object' && type !== null && Object.keys(type).length === 0) {
    6. info += ' You likely forgot to export your component from the file ' + "it's defined in, or you might have mixed up default and named imports.";
    7. }
    8. var sourceInfo = getSourceInfoErrorAddendumForProps(props);
    9. if (sourceInfo) {
    10. info += sourceInfo;
    11. } else {
    12. info += getDeclarationErrorAddendum();
    13. }
    14. var typeString = void 0;
    15. if (type === null) {
    16. typeString = 'null';
    17. } else if (Array.isArray(type)) {
    18. typeString = 'array';
    19. } else if (type !== undefined && type.$$typeof === REACT_ELEMENT_TYPE) {
    20. typeString = '<' + (getComponentName(type.type) || 'Unknown') + ' />';
    21. info = ' Did you accidentally export a JSX literal instead of a component?';
    22. } else {
    23. typeString = typeof type;
    24. }
    25. warning$1(false, 'React.createElement: type is invalid -- expected a string (for ' + 'built-in components) or a class/function (for composite ' + 'components) but got: %s.%s', typeString, info);
    26. }
    27. var element = createElement.apply(this, arguments);
    28. // return null的情况
    29. if (element == null) {
    30. return element;
    31. }
    32. // 如果agrument.length长度为3的话执行
    33. if (validType) {
    34. for (var i = 2; i < arguments.length; i++) {
    35. validateChildKeys(arguments[i], type);
    36. }
    37. }
    38. // 如果节点的类型是fragment, 这种节点比较特殊,它是这样的<></>
    39. if (type === REACT_FRAGMENT_TYPE) {
    40. validateFragmentProps(element);
    41. } else {
    42. // 验证你是一个 一个普通HTMLElement元素,还是一个class元素
    43. validatePropTypes(element);
    44. }
    45. return element;
    46. }
    1. /**
    2. * @description 创建一个ReactElement元素
    3. * @param {*} type 它可以是一个string,或者是一个函数,如果是一个函数的话,会验证它的函数的原型是谁
    4. * @param {*} config props与ref与key的设置
    5. * @param {*} children 子元素,可以是数组,也可以是单个元素
    6. */
    7. function createElement(type, config, children) {
    8. var propName = void 0;
    9. // 设置props
    10. var props = {};
    11. var key = null;
    12. var ref = null;
    13. var self = null;
    14. var source = null;
    15. // 这里是传递给子元素的props的设置,和ref与key的设置,还有__self,__source
    16. if (config != null) {
    17. if (hasValidRef(config)) {
    18. ref = config.ref;
    19. }
    20. if (hasValidKey(config)) {
    21. key = '' + config.key;
    22. }
    23. self = config.__self === undefined ? null : config.__self;
    24. source = config.__source === undefined ? null : config.__source;
    25. for (propName in config) {
    26. // 过滤ref,key,__self,__source
    27. /*
    28. var RESERVED_PROPS = {
    29. key: true,
    30. ref: true,
    31. __self: true,
    32. __source: true
    33. };
    34. */
    35. const bool = !RESERVED_PROPS.hasOwnProperty(propName);
    36. if (hasOwnProperty$1.call(config, propName) && bool) {
    37. props[propName] = config[propName];
    38. }
    39. }
    40. }
    41. // 检测参数个,如果只有3个值,证明children是一个元素,如果多余3个值,children是一个数组,
    42. // 并将它加入到props.chlidren里面
    43. var childrenLength = arguments.length - 2;
    44. if (childrenLength === 1) {
    45. props.children = children;
    46. } else if (childrenLength > 1) {
    47. var childArray = Array(childrenLength);
    48. for (var i = 0; i < childrenLength; i++) {
    49. childArray[i] = arguments[i + 2];
    50. }
    51. {
    52. // 如果支持Object.freeze,将子元素冻结
    53. if (Object.freeze) {
    54. Object.freeze(childArray);
    55. }
    56. }
    57. props.children = childArray;
    58. }
    59. // 如果type存在,并且具备defaultProps属性时,此时type一般为函数类型,将defaultProps的value赋值到props里面,注意这里不是递归的哦,只处理第一层
    60. if (type && type.defaultProps) {
    61. var defaultProps = type.defaultProps;
    62. for (propName in defaultProps) {
    63. if (props[propName] === undefined) {
    64. props[propName] = defaultProps[propName];
    65. }
    66. }
    67. }
    68. // 有没有key或ref
    69. {
    70. if (key || ref) { // 如果key存在或者ref存在
    71. var displayName = typeof type === 'function' ? type.displayName || type.name || 'Unknown' : type;
    72. if (key) {
    73. defineKeyPropWarningGetter(props, displayName);
    74. }
    75. if (ref) {
    76. defineRefPropWarningGetter(props, displayName);
    77. }
    78. }
    79. }
    80. return ReactElement(type, key, ref, self, source, ReactCurrentOwner.current, props);
    81. }
    82. /**
    83. * @description 返回一个ReactElement对象
    84. * @param {*} type
    85. * @param {*} props
    86. * @param {*} key
    87. * @param {string|object} ref
    88. * @param {*} owner
    89. * @param {*} self
    90. * @param {*} source
    91. * @internal
    92. */
    93. var ReactElement = function (type, key, ref, self, source, owner, props) {
    94. var element = {
    95. // This tag allows us to uniquely identify this as a React Element
    96. $$typeof: REACT_ELEMENT_TYPE,
    97. // Built-in properties that belong on the element
    98. type: type,
    99. key: key,
    100. ref: ref,
    101. props: props,
    102. // Record the component responsible for creating this element.
    103. _owner: owner
    104. };
    105. {
    106. // 设置一个react.element的仓库
    107. element._store = {};
    108. // 属性描述符号
    109. // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
    110. // https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties
    111. // 设置属性描述符
    112. Object.defineProperty(element._store, 'validated', {
    113. configurable: false,
    114. enumerable: false,
    115. writable: true,
    116. value: false
    117. });
    118. // 设置属性描述符
    119. Object.defineProperty(element, '_self', {
    120. configurable: false,
    121. enumerable: false,
    122. writable: false,
    123. value: self
    124. });
    125. // 设置属性描述符号
    126. Object.defineProperty(element, '_source', {
    127. configurable: false,
    128. enumerable: false,
    129. writable: false,
    130. value: source
    131. });
    132. // 冻结一个对象,冻结的对象不允许被修改
    133. if (Object.freeze) {
    134. Object.freeze(element.props);
    135. Object.freeze(element);
    136. }
    137. }
    138. return element;
    139. };