React16源码: React中的updateMemoComponent的源码实现

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

updateMemoComponent


1 )概述

  • 在react16.6之后,提供了一个新的API
  • 通过 React.memo 来创建一个具有类似于 pure component 特性的 function component
  • 现在主要关注其更新过程

2 )源码

定位到 packages/react-reconciler/src/ReactFiberBeginWork.js#L237
进入 updateMemoComponent

function updateMemoComponent(current: Fiber | null,workInProgress: Fiber,Component: any,nextProps: any,updateExpirationTime,renderExpirationTime: ExpirationTime,
): null | Fiber {// 进来之后, 也是要先判断current是否等于null// 因为对于current是否等于null, 在update的一个过程当中非常重要// 它是用来辨别我们这个组件是第一次渲染还是非第一次渲染的情况// 这里是 第一次渲染的情况if (current === null) {// 获取 type// 是在创建 memo 的时候,它 return 的是一个 `{ $$typeof: REACT_MEMO_TYPE, type }`// 然后它的type是传进来的 function component// 在这里获取的 type 是一个function componentlet type = Component.type;// 注意,这里,compare 是 调用 memo 时,传递的 第二个参数// 用于对比新老props是否有变化的一个函数, 类似于 SCU// 在我们每一次组件更新的过程当中,都会调用那个方法,判断这个组件是否需要更新if (isSimpleFunctionComponent(type) && Component.compare === null) {// If this is a plain function component without default props,// and with only the default shallow comparison, we upgrade it// to a SimpleMemoComponent to allow fast path updates.workInProgress.tag = SimpleMemoComponent;workInProgress.type = type;// 符合上述 if 条件,会按照 下面这个函数 去更新组件return updateSimpleMemoComponent(current,workInProgress,type,nextProps,updateExpirationTime,renderExpirationTime,);}// 不符合上述条件,第一次渲染的情况,调用 createFiberFromTypeAndProps 获取 child// 不需要调用 reconcileChildren 来减少运行时开销,为什么是这样呢?// 因为它的children是非常明确的,就是 function component// 所以它传入这个type就是function component// nextprops就是放到 memo component上面的 props// 因为这个props实际是要传给具体的 function component// 以及传入 mode 和 renderExpirationTimelet child = createFiberFromTypeAndProps(Component.type,null,nextProps,null,workInProgress.mode,renderExpirationTime,);// 并挂载child.ref = workInProgress.ref;child.return = workInProgress;workInProgress.child = child;return child;}// 对于不是第一次渲染let currentChild = ((current.child: any): Fiber); // This is always exactly one child// 如果组件需要更新if (updateExpirationTime < renderExpirationTime) {// This will be the props with resolved defaultProps,// unlike current.memoizedProps which will be the unresolved ones.const prevProps = currentChild.memoizedProps;// Default to shallow comparisonlet compare = Component.compare;compare = compare !== null ? compare : shallowEqual;// 对比新老 props 和 ref 是否有变化,需要更新,但是没有变化,则跳过更新if (compare(prevProps, nextProps) && current.ref === workInProgress.ref) {// 符合条件,跳过组件更新return bailoutOnAlreadyFinishedWork(current,workInProgress,renderExpirationTime,);}}// React DevTools reads this flag.workInProgress.effectTag |= PerformedWork;// 正常更新// let newChild = createWorkInProgress(currentChild,nextProps,renderExpirationTime,);newChild.ref = workInProgress.ref;newChild.return = workInProgress;workInProgress.child = newChild;return newChild;
}
  • 进入 isSimpleFunctionComponent

    // 它首先判断这个type是否是一个function,以及是否没有 construct 方法,以及它没有 defaultProps 这个属性
    // 也就是说它没有这些类似于 class component 的一些属性
    // 它就是一个纯 function component
    export function isSimpleFunctionComponent(type: any) {return (typeof type === 'function' &&!shouldConstruct(type) &&type.defaultProps === undefined);
    }
    
  • 进入 updateSimpleMemoComponent

    // 在这里传进来的 Component 已经不是我们的 memo component了
    // 而是这个Component里面的type
    function updateSimpleMemoComponent(current: Fiber | null,workInProgress: Fiber,Component: any,nextProps: any,updateExpirationTime,renderExpirationTime: ExpirationTime,
    ): null | Fiber {// 首先是一个常规的判断 current是否等于null 以及它的 updateExpirationTime 相关的一些判断if (current !== null && updateExpirationTime < renderExpirationTime) {// 拿到 prevProps,就是之前的propsconst prevProps = current.memoizedProps;// nextProps 就是现在 fiber 对象上的 pendingProps// 判断两个 props 是否相等,以及两个 ref 是否相等if (shallowEqual(prevProps, nextProps) &&current.ref === workInProgress.ref) {// 跳过组件的更新return bailoutOnAlreadyFinishedWork(current,workInProgress,renderExpirationTime,);}}// 需要更新// 我们增加了memo这个特性,只是说我们可以通过一些简单的判断,跳过组件的更新// 对于纯 function component,每次都需要去执行这个 function component 里面的逻辑的// 可能会造成一些性能上面的开销的问题return updateFunctionComponent(current,workInProgress,Component,nextProps,renderExpirationTime,);
    }
    
  • 我们通过React.memo这个方法创建的组件,它的所有props是直接作用于传入的 function component的

  • 为什么在这里它不使用 reconcileChildFibers 方法呢?

  • 因为 reconcileChildFibers,它会把props作为我自己当前这个组件

    • 也就是我们的 memo component,它的children去进行一个调和的一个过程
    • 最终得到的是我们在使用 memo component 包裹的那一部分的 props.child里面的东西
    • 但是事实上, 在memo组件里面,它的child是来自于 component.type
    • 也就是我们传入的那个 function component,还是它的child
  • 简单总结

    • 我们的 memo component 它拿到的props是作为 Component.type
    • 也就是我们传入的 function component,它的props来进行一个创建他的 childfiber 的一个内容
    • 所以 memo component 的更新方式跟其他的组件会有一定的不同,因为它本身没有任何的一个意义
    • 本身提供的功能只是用来对比新老props是否有变化
    • 所以他接收的其他的props,都是应该作为它的真正的组件也就是function component 的props来进行使用
    • 所以这里面,它通过两个不同的方式,在两个不同的阶段去创建他child的一个过程

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



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

相关文章

Spring Boot 实现 IP 限流的原理、实践与利弊解析

《SpringBoot实现IP限流的原理、实践与利弊解析》在SpringBoot中实现IP限流是一种简单而有效的方式来保障系统的稳定性和可用性,本文给大家介绍SpringBoot实现IP限... 目录一、引言二、IP 限流原理2.1 令牌桶算法2.2 漏桶算法三、使用场景3.1 防止恶意攻击3.2 控制资源

springboot下载接口限速功能实现

《springboot下载接口限速功能实现》通过Redis统计并发数动态调整每个用户带宽,核心逻辑为每秒读取并发送限定数据量,防止单用户占用过多资源,确保整体下载均衡且高效,本文给大家介绍spring... 目录 一、整体目标 二、涉及的主要类/方法✅ 三、核心流程图解(简化) 四、关键代码详解1️⃣ 设置

Nginx 配置跨域的实现及常见问题解决

《Nginx配置跨域的实现及常见问题解决》本文主要介绍了Nginx配置跨域的实现及常见问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来... 目录1. 跨域1.1 同源策略1.2 跨域资源共享(CORS)2. Nginx 配置跨域的场景2.1

Python中提取文件名扩展名的多种方法实现

《Python中提取文件名扩展名的多种方法实现》在Python编程中,经常会遇到需要从文件名中提取扩展名的场景,Python提供了多种方法来实现这一功能,不同方法适用于不同的场景和需求,包括os.pa... 目录技术背景实现步骤方法一:使用os.path.splitext方法二:使用pathlib模块方法三

CSS place-items: center解析与用法详解

《CSSplace-items:center解析与用法详解》place-items:center;是一个强大的CSS简写属性,用于同时控制网格(Grid)和弹性盒(Flexbox)... place-items: center; 是一个强大的 css 简写属性,用于同时控制 网格(Grid) 和 弹性盒(F

CSS实现元素撑满剩余空间的五种方法

《CSS实现元素撑满剩余空间的五种方法》在日常开发中,我们经常需要让某个元素占据容器的剩余空间,本文将介绍5种不同的方法来实现这个需求,并分析各种方法的优缺点,感兴趣的朋友一起看看吧... css实现元素撑满剩余空间的5种方法 在日常开发中,我们经常需要让某个元素占据容器的剩余空间。这是一个常见的布局需求

CSS Anchor Positioning重新定义锚点定位的时代来临(最新推荐)

《CSSAnchorPositioning重新定义锚点定位的时代来临(最新推荐)》CSSAnchorPositioning是一项仍在草案中的新特性,由Chrome125开始提供原生支持需... 目录 css Anchor Positioning:重新定义「锚定定位」的时代来了! 什么是 Anchor Pos

CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比

《CSS中的Static、Relative、Absolute、Fixed、Sticky的应用与详细对比》CSS中的position属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布... css 中的 position 属性用于控制元素的定位方式,不同的定位方式会影响元素在页面中的布局和层叠关

HTML5 getUserMedia API网页录音实现指南示例小结

《HTML5getUserMediaAPI网页录音实现指南示例小结》本教程将指导你如何利用这一API,结合WebAudioAPI,实现网页录音功能,从获取音频流到处理和保存录音,整个过程将逐步... 目录1. html5 getUserMedia API简介1.1 API概念与历史1.2 功能与优势1.3

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja