React16源码: React中commitAllHostEffects内部的commitPlacement的源码实现

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

commitPlacement


1 )概述

  • 在 react commit 阶段的 commitRoot 第二个while循环中
  • 调用了 commitAllHostEffects,在这个函数内部处理了
  • 把一个新的dom节点挂载到真正的dom树上面去的一个过程
  • 现在主要关注下其中调用的 commitPlacement

2 )源码

定位到 packages/react-reconciler/src/ReactFiberCommitWork.js#L850

function commitPlacement(finishedWork: Fiber): void {if (!supportsMutation) {return;}// Recursively insert all host nodes into the parent.// 找到第一个父节点上的 HostComponent 或 HostRoot 或 HostPortalconst parentFiber = getHostParentFiber(finishedWork);// Note: these two variables *must* always be updated together.let parent;let isContainer;// 判断 tag 匹配处理程序switch (parentFiber.tag) {case HostComponent:parent = parentFiber.stateNode;isContainer = false;break;case HostRoot:parent = parentFiber.stateNode.containerInfo;isContainer = true;break;case HostPortal:parent = parentFiber.stateNode.containerInfo;isContainer = true;break;default:invariant(false,'Invalid host parent fiber. This error is likely caused by a bug ' +'in React. Please file an issue.',);}if (parentFiber.effectTag & ContentReset) {// Reset the text content of the parent before doing any insertionsresetTextContent(parent);// Clear ContentReset from the effect tagparentFiber.effectTag &= ~ContentReset;}// before可能不存在,比如如果是单一节点const before = getHostSibling(finishedWork);// We only have the top Fiber that was inserted but we need recurse down its// children to find all the terminal nodes.let node: Fiber = finishedWork;while (true) {// 只有这种情况下,才有插入dom的需要,根据 before 和 isContainer来执行不同的插入if (node.tag === HostComponent || node.tag === HostText) {if (before) {// 这里匹配 HostRoot 或 HostPortalif (isContainer) {insertInContainerBefore(parent, node.stateNode, before);} else {// 这里匹配 HostComponentinsertBefore(parent, node.stateNode, before);}} else {// 不存在 before// 匹配 HostRoot 或 HostPortalif (isContainer) {appendChildToContainer(parent, node.stateNode);} else {// 匹配 HostComponentappendChild(parent, node.stateNode);}}} else if (node.tag === HostPortal) {// If the insertion itself is a portal, then we don't want to traverse// down its children. Instead, we'll get insertions from each child in// the portal directly.} else if (node.child !== null) {// 如果都不符合,并且存在子节点,往下去找node.child.return = node;node = node.child;continue; // 拿到 child 后,跳过此次,继续while循环}// 到终点了,就结束if (node === finishedWork) {return;}while (node.sibling === null) {if (node.return === null || node.return === finishedWork) {return;}node = node.return;}node.sibling.return = node.return;node = node.sibling;}
}
  • 进入 getHostParentFiber
    function getHostParentFiber(fiber: Fiber): Fiber {let parent = fiber.return;while (parent !== null) {if (isHostParent(parent)) {return parent;}parent = parent.return;}invariant(false,'Expected to find a host parent. This error is likely caused by a bug ' +'in React. Please file an issue.',);
    }function isHostParent(fiber: Fiber): boolean {return (fiber.tag === HostComponent ||fiber.tag === HostRoot ||fiber.tag === HostPortal);
    }
    
    • 显然意见,这个循环就是向上查找到第一个 HostComponentHostRootHostPortal
  • 之后,判断 parentFiber.tag,对不同条件的 isContainer 进行赋值
  • 之后,判断 ContentReset 是否存在,存在则对文本节点进行替换
  • 进入 getHostSibling 向上找到 sibling 节点,下面这个英文注释留着 这个是查找sibling的核心算法
    function getHostSibling(fiber: Fiber): ?Instance {// We're going to search forward into the tree until we find a sibling host// node. Unfortunately, if multiple insertions are done in a row we have to// search past them. This leads to exponential search for the next sibling.// TODO: Find a more efficient way to do this.let node: Fiber = fiber;// 这里定义一个while循环叫做 siblingssiblings: while (true) {// If we didn't find anything, let's try the next sibling.// 没有兄弟节点while (node.sibling === null) {if (node.return === null || isHostParent(node.return)) {// If we pop out of the root or hit the parent the fiber we are the// last sibling.return null;}node = node.return; // 向父级溯源}node.sibling.return = node.return; // 这里本就相同node = node.sibling; // 兄弟节点变成当前// 这个循环要找兄弟节点中的第一个dom节点// 如果兄弟节点不是 HostComponent 或 HostText 往下去找// 子节点中的第一个dom节点while (node.tag !== HostComponent && node.tag !== HostText) {// If it is not host node and, we might have a host node inside it.// Try to search down until we find one.// 当前是要替换的节点,就没有必要向下找了if (node.effectTag & Placement) {// If we don't have a child, try the siblings instead.continue siblings;}// If we don't have a child, try the siblings instead.// We also skip portals because they are not part of this host tree.// 像是这种,也没有必要继续找了               if (node.child === null || node.tag === HostPortal) {continue siblings;} else {node.child.return = node;node = node.child;}}// Check if this host node is stable or about to be placed.if (!(node.effectTag & Placement)) {// Found it!return node.stateNode;}}
    }
    
  • 接下去又进入一个while循环
    • 里面的第一个判断,node.tag === HostComponent || node.tag === HostText
    • 只有 HostComponent 和 HostText 才有插入dom的需要
    • 注意,这里一系列的 if-else 是操作dom的核心
  • react真正展现给用户的是一棵dom树,而react中存储的是fiber树
  • 而fiber树不会有每个节点对应的dom节点
  • 以上是操作 commitPlacement 的源码处理,主要关注的是上述while循环和判断

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



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

相关文章

Nginx实现高并发的项目实践

《Nginx实现高并发的项目实践》本文主要介绍了Nginx实现高并发的项目实践,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录使用最新稳定版本的Nginx合理配置工作进程(workers)配置工作进程连接数(worker_co

python中列表list切分的实现

《python中列表list切分的实现》列表是Python中最常用的数据结构之一,经常需要对列表进行切分操作,本文主要介绍了python中列表list切分的实现,文中通过示例代码介绍的非常详细,对大家... 目录一、列表切片的基本用法1.1 基本切片操作1.2 切片的负索引1.3 切片的省略二、列表切分的高

基于Python实现一个PDF特殊字体提取工具

《基于Python实现一个PDF特殊字体提取工具》在PDF文档处理场景中,我们常常需要针对特定格式的文本内容进行提取分析,本文介绍的PDF特殊字体提取器是一款基于Python开发的桌面应用程序感兴趣的... 目录一、应用背景与功能概述二、技术架构与核心组件2.1 技术选型2.2 系统架构三、核心功能实现解析

使用Python实现表格字段智能去重

《使用Python实现表格字段智能去重》在数据分析和处理过程中,数据清洗是一个至关重要的步骤,其中字段去重是一个常见且关键的任务,下面我们看看如何使用Python进行表格字段智能去重吧... 目录一、引言二、数据重复问题的常见场景与影响三、python在数据清洗中的优势四、基于Python的表格字段智能去重

Spring AI集成DeepSeek实现流式输出的操作方法

《SpringAI集成DeepSeek实现流式输出的操作方法》本文介绍了如何在SpringBoot中使用Sse(Server-SentEvents)技术实现流式输出,后端使用SpringMVC中的S... 目录一、后端代码二、前端代码三、运行项目小天有话说题外话参考资料前面一篇文章我们实现了《Spring

Nginx中location实现多条件匹配的方法详解

《Nginx中location实现多条件匹配的方法详解》在Nginx中,location指令用于匹配请求的URI,虽然location本身是基于单一匹配规则的,但可以通过多种方式实现多个条件的匹配逻辑... 目录1. 概述2. 实现多条件匹配的方式2.1 使用多个 location 块2.2 使用正则表达式

使用Apache POI在Java中实现Excel单元格的合并

《使用ApachePOI在Java中实现Excel单元格的合并》在日常工作中,Excel是一个不可或缺的工具,尤其是在处理大量数据时,本文将介绍如何使用ApachePOI库在Java中实现Excel... 目录工具类介绍工具类代码调用示例依赖配置总结在日常工作中,Excel 是一个不可或缺的工http://

SpringBoot实现导出复杂对象到Excel文件

《SpringBoot实现导出复杂对象到Excel文件》这篇文章主要为大家详细介绍了如何使用Hutool和EasyExcel两种方式来实现在SpringBoot项目中导出复杂对象到Excel文件,需要... 在Spring Boot项目中导出复杂对象到Excel文件,可以利用Hutool或EasyExcel

前端bug调试的方法技巧及常见错误

《前端bug调试的方法技巧及常见错误》:本文主要介绍编程中常见的报错和Bug,以及调试的重要性,调试的基本流程是通过缩小范围来定位问题,并给出了推测法、删除代码法、console调试和debugg... 目录调试基本流程调试方法排查bug的两大技巧如何看控制台报错前端常见错误取值调用报错资源引入错误解析错误

Python如何实现读取csv文件时忽略文件的编码格式

《Python如何实现读取csv文件时忽略文件的编码格式》我们再日常读取csv文件的时候经常会发现csv文件的格式有多种,所以这篇文章为大家介绍了Python如何实现读取csv文件时忽略文件的编码格式... 目录1、背景介绍2、库的安装3、核心代码4、完整代码1、背景介绍我们再日常读取csv文件的时候经常