React源码 memo Fragment StrictMode cloneElement createFactory
import {REACT_MEMO_TYPE} from 'shared/ReactSymbols';
export default function memo<Props>(
type: React$ElementType,
compare?: (oldProps: Props, newProps: Props) => boolean,
) {
return {
$$typeof: REACT_MEMO_TYPE,
type,
compare: compare === undefined ? null : compare,
};
}
就是通过 func component 作为参数传入进来,然后可以传第二个参数,是 oldProps 和 newProps 的一个对比方法,return true or false 来判断他是否需要返回,就跟 shouldupdate 是同样的意思,返回一个对象,里面有 $$typeof,是 REACT_MEMO_TYPE。type 就是传入的 function component,然后 compare就是传入的第二个参数,所以跟之前的 ref , context 是一类的东西,最终实现的逻辑肯定是要到 react-dom 里面去实现,他的目的就是为了跟 PureComponent 类似的功能。
Fragment: REACT_FRAGMENT_TYPE
Fragment 就是一个 Symbole,没有什么特别的意义,Fragment 就是 <>,react 16 之后提供了我们 Fragment 这个几点,他非常方便的告诉 react 这里是有多个兄弟节点的,他本身就是一个 Symbol,没有特别的含义
StrictMode: REACT_STRICT_MODE_TYPE
export function cloneElement(element, config, children) {
invariant(
!(element === null || element === undefined),
'React.cloneElement(...): The argument must be a React element, but you passed %s.',
element,
);
let propName;
// Original props are copied
const props = Object.assign({}, element.props);
// Reserved names are extracted
let key = element.key;
let ref = element.ref;
// Self is preserved since the owner is preserved.
const self = element._self;
// Source is preserved since cloneElement is unlikely to be targeted by a
// transpiler, and the original source is probably a better indicator of the
// true owner.
const source = element._source;
// Owner will be preserved, unless ref is overridden
let owner = element._owner;
if (config != null) {
if (hasValidRef(config)) {
// Silently steal the ref from the parent.
ref = config.ref;
owner = ReactCurrentOwner.current;
}
if (hasValidKey(config)) {
key = '' + config.key;
}
// Remaining properties override existing props
let defaultProps;
if (element.type && element.type.defaultProps) {
defaultProps = element.type.defaultProps;
}
for (propName in config) {
if (
hasOwnProperty.call(config, propName) &&
!RESERVED_PROPS.hasOwnProperty(propName)
) {
if (config[propName] === undefined && defaultProps !== undefined) {
// Resolve default props
props[propName] = defaultProps[propName];
} else {
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];
}
props.children = childArray;
}
return ReactElement(element.type, key, ref, self, source, owner, props);
}
const props = Object.assign({}, element.props); 首先他把 props 复制过来,然后把 key 和 ref 也复制过来。这些东西复制过来之后再进行一个处理,其实就是创建一个新的 react element , 整体的过程个你 createElemnt 差不多。只是传入一个 element,他进行一个 clone 这么一个过程。
export function createFactory(type) {
const factory = createElement.bind(null, type);
// Expose the type on the factory and the prototype so that it can be
// easily accessed on elements. E.g. `<Foo />.type === Foo`.
// This should not be named `constructor` since this may not be the function
// that created the element, and it may not even be a constructor.
// Legacy hook: remove it
factory.type = type;
return factory;
}
createFactory 对于写 JSX 的人来说,几乎是不可能用到的,因为 createFactory 是对 createElement 的一个封装。createFactory 其实是 createElement 绑定了一个 type。比如我们要去创建一个 p 标签的节点, p 标签的节点如果使用 javascript 的 api 去创建,那么在使用 createElement 的时候都要先传入 p ,再传入 config ,再传入 children 。我们可以先创建一个 p 标签的 factory ,然后通过这个 factory 返回的方法,我们只需要传入 config,children 就可以创建一个 p 标签。而不需要重复的去传 p。但对于写 jsx 的人来说,没有任何的必要。
React源码 memo Fragment StrictMode cloneElement createFactory的更多相关文章
- React躬行记(16)——React源码分析
React可大致分为三部分:Core.Reconciler和Renderer,在阅读源码之前,首先需要搭建测试环境,为了方便起见,本文直接采用了网友搭建好的环境,React版本是16.8.6,与最新版 ...
- React源码剖析系列 - 生命周期的管理艺术
目前,前端领域中 React 势头正盛,很少能够深入剖析内部实现机制和原理.本系列文章希望通过剖析 React 源码,理解其内部的实现原理,知其然更要知其所以然. 对于 React,其组件生命周期(C ...
- React源码解析:ReactElement
ReactElement算是React源码中比较简单的部分了,直接看源码: var ReactElement = function(type, key, ref, self, source, owne ...
- React 源码剖析系列 - 生命周期的管理艺术
目前,前端领域中 React 势头正盛,很少能够深入剖析内部实现机制和原理. 本系列文章 希望通过剖析 React 源码,理解其内部的实现原理,知其然更要知其所以然. 对于 React,其组件生命周期 ...
- react 源码之setState
今天看了react源码,仅以记录. 1:monorepo (react 的代码管理方式) 与multirepo 相对. monorepo是单代码仓库, 是把所有相关项目都集中在一个代码仓库中,每个mo ...
- React 源码剖析系列 - 不可思议的 react diff
简单点的重复利用已有的dom和其他REACT性能快的原理. key的作用和虚拟节点 目前,前端领域中 React 势头正盛,使用者众多却少有能够深入剖析内部实现机制和原理. 本系列文章希望通过剖析 ...
- 读react源码准备
git源码地址:https://github.com/facebook/react react 里面就是 react源码 react里面的react文件夹就是react源码,react源码非常的少,总 ...
- react源码之render
1.最近学习react源码,刚刚入门,看了render的原理,到了fiberRoot的创建 如图:
- React源码之组件的实现与首次渲染
react: v15.0.0 本文讲 组件如何编译 以及 ReactDOM.render 的渲染过程. babel 的编译 babel 将 React JSX 编译成 JavaScript. 在 ba ...
随机推荐
- nowcoder911J 异或的路径
题目链接 题意 给出一棵树,每条边有边权.求\(\sum\limits_{i=1}^n{f(i,j)}\),\(f(i,j)\)表示从i到j路径的异或和. 思路 \(g_i\)表示从根到\(i\)的异 ...
- PKUWC2020游记
PKUWC2020在2019年举行是真的沙雕 晚个两星期我就能逃掉期末考了 Day \(-\infty\) 开始停课训练,和ntf.lzy一起. atcoder真好玩,只可惜我没脑子-- kenkoo ...
- 简单模仿QQ聊天界面
首先看一下最终的效果,显示了消息时间,用户昵称,用户头像. 大致实现方法: 用最简单的ListView显示消息内容. 不同的用户使用不同的消息布局文件,从而达到头像左右显示的效果,如上图有2个用户&q ...
- JVM系列之四:运行时数据区
1. JVM架构图 Java虚拟机主要分为五大模块:类装载器子系统.运行时数据区.执行引擎.本地方法接口和垃圾收集模块. 2. JDK1.7内存模型-运行时数据区域 根据<Java 虚拟机规范( ...
- C#原型模式(深拷贝、浅拷贝)
原型模式就是用于创建重复的对象,当想要创建一个新的对象但是开销比较大或者想将对象的当前状态保存下来的时候,我们就可以使用原型模式. 创建原型 public abstract class Base { ...
- K8S 如何实现将git代码下拉到指定的容器路径中
gitRepo 是 kubernetes Volume类型中的一种,gitRepo volume可以实现将git代码下拉到指定的容器路径中. 备注:实现此功能,Pod运行的节点都必需要安装git.换句 ...
- Java内部类是如何实现的
内部类(inner class)是定义在另一个类中的类. 内部类方法可以访问该类定义所在的作用域中的数据,包括私有的数据. 内部类可以对同一个包中的其他类隐藏起来 当想定义一个回调函数且不想编写大量代 ...
- 百度前端技术学院-task1.4源代码
任务描述 实现如 示例图(点击打开) 的效果 灰色元素水平垂直居中,有两个四分之一圆位于其左上角和右下角. 任务注意事项 思考不同情况下(如灰色高度是根据内容动态变化的)水平垂直居中的解决方案. 动手 ...
- 《 .NET并发编程实战》阅读指南 - 第11章
先发表生成URL以印在书里面.等书籍正式出版销售后会公开内容.
- python输入一个字符串,输出翻转后的字符串(翻转字符串)
题目:输出一个字符串,输出翻转后的字符串.例如:输入字符串a123,输出321a. 方法一:使用列表的reverse方法 string=input('请输入一个字符串:') lst=list(stri ...