ReactElement 源码笔记

ReactElement通过 createElement创建,调用该方法需要 传入三个参数:

  • type
  • config
  • children

type指代这个ReactElement 的类型

config指代这个ReactElement 的属性对象

children指代这个ReactElement 的子元素节点

  • 字符串比如 'div''p'代表原生DOM,称为HostComponent
  • Class 类型是我们继承自 Component 或者 PureComponent的组件,称为ClassComponent
  • 方法就是 function Component
  • 原生提供的 FragmentAsyncMode 等是 Symbol,会被特殊处理
  • TODO: 是否有其他的
//源码位置: packages/react/src/ReactElement.js
// createElement函数
export function createElement(type, config, children){
// 一、处理config // 1. 判断config是否为空 // 2. 提取保留属性(key, ref, self, soure) // 3. 其余属性添加到新的props对象中
for(propName in config){
if(
hasOwnProperty.call(config, propName)&&
!RESERVED_PROPS.hasOwnProperty(propName)
){
props[propName] = config[propName]
}
} // 二、处理children => props.children
// (children可以超过一个),处理过后的children 被放入 props.children
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];
}
props.children = childArray;
}
// 三、处理type的 defaultProps
if(type && type.defaultProps) {
const defaultProps = type.defaultProps;
for (propName in defaultProps) {
if(props[propName] === undefined) {
props[propName] = defaultProps[propName]
}
}
} // 四、返回一个ReactElement()函数
return ReactElement(
type,
key,
ref,
self,
source,
ReactCurrentOwner.current,
props,
);
}

注:type.defaultProps的由来:

  1. 定义一个ClassComponent: class Comp extends React.Component
  2. 可以为 Comp设置一个defaultProps: Comp.defaultProps = { value: 1 }
//源码位置: packages/react/src/ReactElement.js
// ReactElement 函数
const ReactElement = function(type, key, ref, self, source, owner, props){
const element = {
// $$typeof用于标识 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,
} // _DEV_ 代码... return element;
}

最新文章

  1. idea缓存
  2. 【Python实战】Pandas:让你像写SQL一样做数据分析(一)
  3. UIButton 的点击事件详解
  4. 未能加载文件或程序集&quot;Microsoft.Web.Infrastructure 的解决方案
  5. IE8兼容H5语义标签
  6. SQL union和union all的区别
  7. 239. Sliding Window Maximum
  8. python 常用模块
  9. 静态文件服务器(The static file servers)
  10. Visual Studio 2015安装过程卡住,解决办法
  11. 网站优化与Cdn文件传输服务
  12. JS将文件以form表单一样提交到后台
  13. did not finish being created even after we waited 189 seconds or 61 attempts. And its status is downloading
  14. windows安装oracle遇INS 30131 错误
  15. Maven项目中遇到的问题及其解决方案
  16. python,关于用户登录与注册问题
  17. wget安装pip和pip3
  18. nginx支持ssl双向认证配置
  19. 网站SEO优化的方法
  20. 关于YII中layout中的布局和view中数据的关系

热门文章

  1. Codeforces Global Round 7 C. Permutation Partitions(组合数学)
  2. 博弈论入门——Nim游戏引入
  3. 【poj 2976】Dropping tests(算法效率--01分数规划 模版题+二分){附【转】01分数规划问题}
  4. Codeforces Round #531 (Div. 3) E. Monotonic Renumeration (构造)
  5. C# EventWaitHandle类解析
  6. 再记一次 应用服务器 CPU 暴高事故分析
  7. Kubernets二进制安装(11)之部署Node节点服务的kubelet
  8. HTML——标签使用
  9. codeforces 868B
  10. 最新 uni-app 免费教程