当前位置: 首页 > news >正文

React16源码: React中的completeWork对HostText处理含更新的源码实现

HostText


1 )概述

  • completeWork 中 对 HostText的处理
  • 在第一次挂载和后续更新的不同条件下进行操作
    • 第一次挂载主要是创建实例
    • 后续更新其实也是重新创建实例

2 )源码

定位到 packages/react-reconciler/src/ReactFiberCompleteWork.js#L663

case HostText 这里

case HostText: {let newText = newProps;// 符合这个条件,说明它不是第一次渲染,就是更新的状态// 调用 updateHostText 进行更新if (current && workInProgress.stateNode != null) {const oldText = current.memoizedProps;// If we have an alternate, that means this is an update and we need// to schedule a side-effect to do the updates.updateHostText(current, workInProgress, oldText, newText);} else {// 对于第一次渲染if (typeof newText !== 'string') {invariant(workInProgress.stateNode !== null,'We must have new props for new mounts. This error is likely ' +'caused by a bug in React. Please file an issue.',);// This can happen when we abort work.}// 跳过 context 处理const rootContainerInstance = getRootHostContainer();const currentHostContext = getHostContext();// 跳过 hydrate 处理let wasHydrated = popHydrationState(workInProgress);if (wasHydrated) {if (prepareToHydrateHostTextInstance(workInProgress)) {markUpdate(workInProgress);}} else {// 创建 文本 实例workInProgress.stateNode = createTextInstance(newText,rootContainerInstance,currentHostContext,workInProgress,);}}break;
}
  • 进入 updateHostText

    updateHostText = function(current: Fiber,workInProgress: Fiber,oldText: string,newText: string,
    ) {// 这个非常简单,通过前后 text 是否有区别// 如果不同,则创建新的 text实例if (oldText !== newText) {// If the text content differs, we'll create a new text instance for it.// 先跳过 context 的处理const rootContainerInstance = getRootHostContainer();const currentHostContext = getHostContext();workInProgress.stateNode = createTextInstance(newText,rootContainerInstance,currentHostContext,workInProgress,);// We'll have to mark it as having an effect, even though we won't use the effect for anything.// This lets the parents know that at least one of their children has changed.markUpdate(workInProgress);}
    };
    
    • 进入 createTextInstance
      // packages/react-dom/src/client/ReactDOMHostConfig.js#L272
      export function createTextInstance(text: string,rootContainerInstance: Container,hostContext: HostContext,internalInstanceHandle: Object,
      ): TextInstance {if (__DEV__) {const hostContextDev = ((hostContext: any): HostContextDev);validateDOMNesting(null, text, hostContextDev.ancestorInfo);}const textNode: TextInstance = createTextNode(text, rootContainerInstance);// 这个方法之前阅过precacheFiberNode(internalInstanceHandle, textNode);return textNode;
      }
      
      • 进入 createTextNode
        export function createTextNode(text: string,rootContainerElement: Element | Document,
        ): Text {// 最终 getOwnerDocumentFromRootContainer 这里返回一个dom对象,调用dom的 createTextNode 这个方法return getOwnerDocumentFromRootContainer(rootContainerElement).createTextNode(text,);
        }
        
        • 进入 getOwnerDocumentFromRootContainer
        function getOwnerDocumentFromRootContainer(rootContainerElement: Element | Document,
        ): Document {// rootContainerElement.ownerDocument 这里是 window.document 对象// 这么做是为了兼容多平台api的使用return rootContainerElement.nodeType === DOCUMENT_NODE? (rootContainerElement: any): rootContainerElement.ownerDocument;
        }
        
  • HostText 相比于 HostComponents 来说,它没有多种选择

  • 没有各种各样的属性,整体来说非常的简单

  • 一些特别说明的,写在了上述代码注释中

http://www.lryc.cn/news/287113.html

相关文章:

  • 网络协议与攻击模拟_07UDP协议
  • 生命在于折腾——WeChat机器人的研究和探索
  • 融资项目——EasyExcel将Excel文件保存至数据库
  • 【Oracle】设置FGA(Fine-Grained Audit)细粒度审计
  • js vue调用activex ocx
  • Hbas简介:数据模型和概念、物理视图
  • uniapp css样式穿透
  • 【立创EDA-PCB设计基础完结】7.DRC设计规则检查+优化与丝印调整+打样与PCB生产进度跟踪
  • android 线程池的管理工具类
  • 编码风格之(5)GNU软件编码风格(3)
  • 8 种网络协议
  • Flash读取数据库中的数据
  • 如何写出规范优雅的代码
  • 【数据结构】链表(单链表与双链表实现+原理+源码)
  • 14027.ptp 控制流
  • 【昕宝爸爸小模块】深入浅出之为什么POI的SXSSFWorkbook占用内存更小
  • CentOS安装Flume
  • Qt 多次绘图
  • 设计模式介绍
  • linux 之 ln 命令
  • 【设计模式】张一鸣笔记:责任链接模式怎么用?
  • Vulnhub-dc4
  • MySQL45道练习题
  • HTML5和CSS3的新特性
  • 【MySQL】表列数和行大小限制详解
  • 算法基础学习|双指针算法
  • 4.远程登录服务
  • 代码随想录算法训练营第二十九天| 491.递增子序列、46.全排列、47.全排列 II
  • 基于若依的ruoyi-nbcio流程管理系统一种简单的动态表单模拟测试实现(五)
  • 多场景建模:阿里多场景多任务元学习方法M2M