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

相关文章

vue基于ElementUI动态设置表格高度的3种方法

《vue基于ElementUI动态设置表格高度的3种方法》ElementUI+vue动态设置表格高度的几种方法,抛砖引玉,还有其它方法动态设置表格高度,大家可以开动脑筋... 方法一、css + js的形式这个方法需要在表格外层设置一个div,原理是将表格的高度设置成外层div的高度,所以外层的div需要

Java中使用Java Mail实现邮件服务功能示例

《Java中使用JavaMail实现邮件服务功能示例》:本文主要介绍Java中使用JavaMail实现邮件服务功能的相关资料,文章还提供了一个发送邮件的示例代码,包括创建参数类、邮件类和执行结... 目录前言一、历史背景二编程、pom依赖三、API说明(一)Session (会话)(二)Message编程客

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

C#提取PDF表单数据的实现流程

《C#提取PDF表单数据的实现流程》PDF表单是一种常见的数据收集工具,广泛应用于调查问卷、业务合同等场景,凭借出色的跨平台兼容性和标准化特点,PDF表单在各行各业中得到了广泛应用,本文将探讨如何使用... 目录引言使用工具C# 提取多个PDF表单域的数据C# 提取特定PDF表单域的数据引言PDF表单是一

使用Python实现高效的端口扫描器

《使用Python实现高效的端口扫描器》在网络安全领域,端口扫描是一项基本而重要的技能,通过端口扫描,可以发现目标主机上开放的服务和端口,这对于安全评估、渗透测试等有着不可忽视的作用,本文将介绍如何使... 目录1. 端口扫描的基本原理2. 使用python实现端口扫描2.1 安装必要的库2.2 编写端口扫

PyCharm接入DeepSeek实现AI编程的操作流程

《PyCharm接入DeepSeek实现AI编程的操作流程》DeepSeek是一家专注于人工智能技术研发的公司,致力于开发高性能、低成本的AI模型,接下来,我们把DeepSeek接入到PyCharm中... 目录引言效果演示创建API key在PyCharm中下载Continue插件配置Continue引言

MySQL分表自动化创建的实现方案

《MySQL分表自动化创建的实现方案》在数据库应用场景中,随着数据量的不断增长,单表存储数据可能会面临性能瓶颈,例如查询、插入、更新等操作的效率会逐渐降低,分表是一种有效的优化策略,它将数据分散存储在... 目录一、项目目的二、实现过程(一)mysql 事件调度器结合存储过程方式1. 开启事件调度器2. 创

使用Python实现操作mongodb详解

《使用Python实现操作mongodb详解》这篇文章主要为大家详细介绍了使用Python实现操作mongodb的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、示例二、常用指令三、遇到的问题一、示例from pymongo import MongoClientf

SQL Server使用SELECT INTO实现表备份的代码示例

《SQLServer使用SELECTINTO实现表备份的代码示例》在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误,在SQLServer中,可以使用SELECTINT... 在数据库管理过程中,有时我们需要对表进行备份,以防数据丢失或修改错误。在 SQL Server 中,可以使用 SE

基于Go语言实现一个压测工具

《基于Go语言实现一个压测工具》这篇文章主要为大家详细介绍了基于Go语言实现一个简单的压测工具,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录整体架构通用数据处理模块Http请求响应数据处理Curl参数解析处理客户端模块Http客户端处理Grpc客户端处理Websocket客户端