React16源码: memo, Fragment, StrictMode, cloneElement, createFactory源码实现

本文主要是介绍React16源码: memo, Fragment, StrictMode, cloneElement, createFactory源码实现,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

memo


1 ) 概述

  • memo 在react 16.6 推出的一个API
  • 它的用意是让 function component,有一个类似 PureComponent 的一个功能
    • PureComponent 提供了 class component 组件类型
    • 在props没有变化的情况下,它可以不重新渲染
  • 目的是给 function component 做一个 PureComponent 的对标
  • 这个用法很简单,就不进行举例了

2 ) 源码解析

// memo.js/*** Copyright (c) Facebook, Inc. and its affiliates.** This source code is licensed under the MIT license found in the* LICENSE file in the root directory of this source tree.*/import {REACT_MEMO_TYPE} from 'shared/ReactSymbols';import isValidElementType from 'shared/isValidElementType';
import warningWithoutStack from 'shared/warningWithoutStack';export default function memo<Props>(type: React$ElementType,compare?: (oldProps: Props, newProps: Props) => boolean,
) {if (__DEV__) {if (!isValidElementType(type)) {warningWithoutStack(false,'memo: The first argument must be a component. Instead ' +'received: %s',type === null ? 'null' : typeof type,);}}return {$$typeof: REACT_MEMO_TYPE,type,compare: compare === undefined ? null : compare,};
}
  • 可以看到 memo 是一个方法
  • 第一个参数 type 可以是 function component
  • 第二个参数 compare
    • 是一个 old props 和 new props 的对比方法
    • 返回值是 boolean,类似于 SCU
  • 最终的返回值是一个对象
    • $$typeofREACT_MEMO_TYPE
    • type,就是我们传入的 function component
    • compare 是我们传入的第3个参数
    • 所以它就跟之前的 forwardRef, context 差不多一类的东西
    • 最终它实现的逻辑肯定还是要到 react-dom 那边来实现的

Fragment


1 ) 概述

  • Fragment 实际上是一个 Symbol
  • 在我们项目代码中的 <></> 实际上等价于 <React.Fragment><React.Fragment />
  • 这是React 渲染函数 render 中返回的单个节点的约束和无用div节点的平衡处理

2 )源码

// React.jsFragment: REACT_FRAGMENT_TYPE,
  • 从概述中得知,本身这个节点没有任何的意义
  • 它也不会生成多余的节点,只是告诉 react 里面是有多个兄弟节点
  • 本身就是一个 Symbol,没有特殊的含义
  • 它的作用就是用于包裹节点

StrictMode


1 ) 概述

  • StrictMode 本质是一个组件
  • 它的作用是在渲染内部组件时,发现不合适的代码并给出提醒

2 )源码

  // React.jsStrictMode: REACT_STRICT_MODE_TYPE,
  • 实际上它也是一个 Symbol
  • 它就跟 ConcurrentMode 是差不多的意思
  • 它这个节点下面的所有子树都要按照某一种模式进行渲染
  • 对于其规则下面的所有子树的节点,会给我们提供一些过时的API的提醒
    • 比如说在某个节点下面,使用了 componentWilMount 这种将要过期的生命周期方法的时候
    • 它就会做出提醒,说你这个方法是不好的,你不应该这么去做
    • 它使用方式也是像 react component,所以影响的范围仅仅是它的子树
  • 类似于 ConcurrentMode, 在它的节点下面才会是有一个异步渲染情况

cloneElement


1 ) 概述

  • 对原 ReactElement 进行克隆处理
  • 返回一个新的ReactElement

2 )源码分析

直接定位在 ReactElement.js

  // ReactElement.jsexport 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 copiedconst props = Object.assign({}, element.props);// Reserved names are extractedlet 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 overriddenlet 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 propslet 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 propsprops[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);}
  • 首先,它把props首先复制过来,const props = Object.assign({}, element.props);
  • 然后呢把key和 ref 也复制过来 然后再进行一些处理
  • 其实就是创建一个新的 React Element
  • 其实整体的过程是跟 createElement 是差不多的
  • 只不过它是传入了一个 element,然后他进行一个克隆这么一个过程

createFactory


1 ) 概述

  • 返回一个某种类型的 ReactElement 工厂函数
  • 可以利用返回的函数来创建一个 ReactElement

2 )源码分析

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 itfactory.type = type;return factory;
}
  • 这个源码比较简洁
  • createFactory 对于写 jsx 的场景几乎是不可能用到的
  • 因为 createFactory 是对 createElement 的一个封装
    • createFactorycreateElement 绑定了一个type
    • 比如说我们要去创建一个p标签的节点
      • 如果使用 JS API去创建,比如使用 createElement
      • 每次都要先传入 p,然后再传入 config,再传入它的children
  • 其实,我们可以先创建一个p标签的 factory
    • 通过这个factory返回的方法,我们只需要传入config和children,就可以创建一个p标签
    • 而不需要重复的去传p这个字符串来表示我们要创建的是p标签的节点

这篇关于React16源码: memo, Fragment, StrictMode, cloneElement, createFactory源码实现的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/564767

相关文章

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

在 Spring Boot 中实现异常处理最佳实践

《在SpringBoot中实现异常处理最佳实践》本文介绍如何在SpringBoot中实现异常处理,涵盖核心概念、实现方法、与先前查询的集成、性能分析、常见问题和最佳实践,感兴趣的朋友一起看看吧... 目录一、Spring Boot 异常处理的背景与核心概念1.1 为什么需要异常处理?1.2 Spring B

Python位移操作和位运算的实现示例

《Python位移操作和位运算的实现示例》本文主要介绍了Python位移操作和位运算的实现示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 位移操作1.1 左移操作 (<<)1.2 右移操作 (>>)注意事项:2. 位运算2.1

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

利用python实现对excel文件进行加密

《利用python实现对excel文件进行加密》由于文件内容的私密性,需要对Excel文件进行加密,保护文件以免给第三方看到,本文将以Python语言为例,和大家讲讲如何对Excel文件进行加密,感兴... 目录前言方法一:使用pywin32库(仅限Windows)方法二:使用msoffcrypto-too