ReactElement源码笔记
ReactElement
源码笔记
ReactElement
通过 createElement
创建,调用该方法需要 传入三个参数:
- type
- config
- children
type
指代这个ReactElement
的类型
config
指代这个ReactElement
的属性对象
children
指代这个ReactElement
的子元素节点
- 字符串比如
'div'
,'p'
代表原生DOM,称为HostComponent
- Class 类型是我们继承自
Component
或者PureComponent
的组件,称为ClassComponent
- 方法就是
function Component
- 原生提供的
Fragment
、AsyncMode
等是 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
的由来:
- 定义一个ClassComponent:
class Comp extends React.Component
- 可以为
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;
}
ReactElement源码笔记的更多相关文章
- Zepto源码笔记(一)
最近在研究Zepto的源码,这是第一篇分析,欢迎大家继续关注,第一次写源码笔记,希望大家多指点指点,第一篇文章由于首次分析原因不会有太多干货,希望后面的文章能成为各位大大心目中的干货. Zepto是一 ...
- redis源码笔记(一) —— 从redis的启动到command的分发
本作品采用知识共享署名 4.0 国际许可协议进行许可.转载联系作者并保留声明头部与原文链接https://luzeshu.com/blog/redis1 本博客同步在http://www.cnblog ...
- AsyncTask源码笔记
AsyncTask源码笔记 AsyncTask在注释中建议只用来做短时间的异步操作,也就是只有几秒的操作:如果是长时间的操作,建议还是使用java.util.concurrent包中的工具类,例如Ex ...
- Java Arrays 源码 笔记
Arrays.java是Java中用来操作数组的类.使用这个工具类可以减少平常很多的工作量.了解其实现,可以避免一些错误的用法. 它提供的操作包括: 排序 sort 查找 binarySearch() ...
- Tomcat8源码笔记(八)明白Tomcat怎么部署webapps下项目
以前没想过这么个问题:Tomcat怎么处理webapps下项目,并且我访问浏览器ip: port/项目名/请求路径,以SSM为例,Tomcat怎么就能将请求找到项目呢,项目还是个文件夹类型的? Tom ...
- Tomcat8源码笔记(七)组件启动Server Service Engine Host启动
一.Tomcat启动的入口 Tomcat初始化简单流程前面博客介绍了一遍,组件除了StandardHost都有博客,欢迎大家指文中错误.Tomcat启动类是Bootstrap,而启动容器启动入口位于 ...
- Tomcat8源码笔记(六)连接器Connector分析
根据 Tomcat8源码笔记(五)组件Container分析 前文分析,StandardService的初始化重心由 StandardEngine转移到了Connector的初始化,本篇记录下Conn ...
- Tomcat8源码笔记(五)组件Container分析
Tomcat8源码笔记(四)Server和Service初始化 介绍过Tomcat中Service的初始化 最先初始化就是Container,而Container初始化过程是咋样的? 说到Contai ...
- Tomcat8源码笔记(四)Server和Service初始化
上一章 简单说明下Tomcat各个组件: Server:服务器,Tomcat服务器,一个Tomcat只有一个Server组件; Service:业务层,是Server下最大的子容器,一个Server可 ...
随机推荐
- hdu 6867 Tree 2020 Multi-University Training Contest 9 dfs+思维
题意: 给你一个由n个点,n-1条有向边构成的一颗树,1为根节点 下面会输入n-1个数,第i个数表示第i+1点的父节点.你可以去添加一条边(你添加的边也是有向边),然后找出来(x,y)这样的成对节点. ...
- python+selenium+bs4爬取百度文库内文字 && selenium 元素可以定位到,但是无法点击问题 && pycharm多行缩进、左移
先说一下可能用到的一些python知识 一.python中使用的是unicode编码, 而日常文本使用各类编码如:gbk utf-8 等等所以使用python进行文字读写操作时候经常会出现各种错误, ...
- 网易云音乐JS逆向解析歌曲链接
Request URL: https://music.163.com/weapi/song/enhance/player/url?csrf_token= FormData : params: BV ...
- Ansible主机清单Inventory文件hosts
Ansible主机清单Inventory文件hosts 发表于 2017-05-14 | 分类于 运维相关 , Ansible | | 阅读次数 4638 | 字数统计 1,442 | 阅读时长预计 ...
- leetcode5 最长回文字符串 动态规划 Manacher法
dp 注意没有声明S不空,处理一下 o(n^2) class Solution { public: string longestPalindrome(string s) { if (s.empty() ...
- codeforces 8C(非原创)
C. Looking for Order time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- int、long long等的取值范围
unsigned int 0-4294967295 int -2147483648-2147483647 unsigned long 0-4294967295long -21474 ...
- 使用Benchmark.NET测试代码性能
今天,我们将研究如何使用Benchmark.Net来测试代码性能.借助基准测试,我们可以创建基准来验证所做的更改是否按预期工作并且不会导致性能下降. 并非每个项目都需要进行基准测试,但是如果您正在开发 ...
- linux & node & cli & exit(0) & exit(1)
linux & node & cli & exit(0) & exit(1) exit(0) & exit(1) demo exit(0) === OK exi ...
- TweenMax & GSAP & SVG Animation
TweenMax & GSAP & SVG Animation svg animation https://greensock.com/tweenmax GSAP https://ww ...