React 16 源码瞎几把解读 【三 点 二】 react中的fiberRoot
〇、先来看看常用的常量
NoWork = 0
noTimeout = undefined
HostRoot = 3
NoContext = 0b000;
AsyncMode = 0b001;
StrictMode = 0b010;
ProfileMode = 0b100;
NoEffect = /* */ 0b00000000000
enableProfilerTimer = __PROFILE__
__PROFILE__: true
isDevToolsPresent = typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined'
一、制造root._internalRoot 的 createFiberRoot 方法
export function createFiberRoot(
containerInfo: any, // id=app 的 dom
isAsync: boolean, // false
hydrate: boolean, // false
): FiberRoot {
const uninitializedFiber = createHostRootFiber(isAsync);
const root = {
current: uninitializedFiber, // 添加fiber 属性 一直用它
containerInfo: containerInfo, // dom app
pendingChildren: null, earliestPendingTime: NoWork,
latestPendingTime: NoWork,
earliestSuspendedTime: NoWork,
latestSuspendedTime: NoWork,
latestPingedTime: NoWork, didError: false, pendingCommitExpirationTime: NoWork,
finishedWork: null,
timeoutHandle: noTimeout,
context: null,
pendingContext: null,
hydrate, //false
nextExpirationTimeToWorkOn: NoWork,
expirationTime: NoWork,
firstBatch: null,
nextScheduledRoot: null,
};
uninitializedFiber.stateNode = root;
return root;
}
通过上面的代码得知,造出来的root._internalRoot 是一个包含若干属性的对象,其中最重要的是一个名叫 current的属性,这个current属性在上一集我们说到虚拟dom渲染过程的时候被当做第一参数 传入了 渲染核心函数 enqueueUpdate中。
这个current属性是个核心
二、 制造current属性的createHostRootFiber(isAsync)方法
export function createHostRootFiber(
isAsync: boolean // false
): Fiber {
// export type TypeOfMode = number;
// export const NoContext = 0b000;
// export const AsyncMode = 0b001;
// export const StrictMode = 0b010;
// export const ProfileMode = 0b100;
let mode = isAsync ? AsyncMode | StrictMode : NoContext; // 0b000
// __PROFILE__: true,
// export const enableProfilerTimer = __PROFILE__;
// export const isDevToolsPresent = typeof __REACT_DEVTOOLS_GLOBAL_HOOK__ !== 'undefined';
if (enableProfilerTimer && isDevToolsPresent) {
// Always collect profile timings when DevTools are present.
// This enables DevTools to start capturing timing at any point–
// Without some nodes in the tree having empty base times.
mode |= ProfileMode; // 0b100
}
// export const HostRoot = 3; // Root of a host tree. Could be nested inside another node.
return createFiber(HostRoot, null, null, mode);
}
这些代码无非是通过入参 isAsync 确定了mode的值,用来决定创造出的current是个具有根节点属性的东西
三、通用方法createFiber -> new FiberNode(...)
createFiber方法只是简单的返回了 new FiberNode(...),我们只需要看FiberNode是个啥
function FiberNode(
tag: TypeOfWork,
pendingProps: mixed,
key: null | string,
mode: TypeOfMode,
) {
// Instance
this.tag = tag;
this.key = key;
this.type = null;
this.stateNode = null; // Fiber
this.return = null;
this.child = null;
this.sibling = null;
this.index = 0; this.ref = null; this.pendingProps = pendingProps;
this.memoizedProps = null;
this.updateQueue = null;
this.memoizedState = null;
this.firstContextDependency = null; this.mode = mode; // Effects
// export const NoEffect = /* */ 0b00000000000;
this.effectTag = NoEffect;
this.nextEffect = null; this.firstEffect = null;
this.lastEffect = null; this.expirationTime = NoWork; //
this.childExpirationTime = NoWork; // this.alternate = null; if (enableProfilerTimer) {
this.actualDuration = 0;
this.actualStartTime = 0;
this.selfBaseDuration = 0;
this.treeBaseDuration = 0;
}
}
附加了一堆将来fiber架构需要用到的属性,具体这些属性是干啥用的,咱们以后再分析
不知道大家还记不记得这个root从我们调用reactDOM.render 传入那个container 往上附加了多少有用的东西,现在来回顾一下:
container 就是咱们传入的那个真实dom
container = {
_reactRootContainer :{ // legacyCreateRootFromDOMContainer
_internalRoot: { // DOMRenderer.createContainer
current:{} // new FiberNode
}
}
}
在dom上附加了这么一串有用的对象
React 16 源码瞎几把解读 【三 点 二】 react中的fiberRoot的更多相关文章
- React 16 源码瞎几把解读 【二】 react组件的解析过程
一.一个真正的react组件编译后长啥样? 我们瞎几把解读了react 虚拟dom对象是怎么生成的,生成了一个什么样的解构.一个react组件不光由若干个这些嵌套的虚拟dom对象组成,还包括各种生命周 ...
- React 16 源码瞎几把解读 【三 点 一】 把react组件对象弄到dom中去(矛头指向fiber,fiber不解读这个过程也不知道)
一.ReactDOM.render 都干啥了 我们在写react的时候,最后一步肯定是 ReactDOM.render( <div> <Home name="home&qu ...
- React 16 源码瞎几把解读 【一】 从jsx到一个react 虚拟dom对象
一.jsx变createElement 每一个用jsx语法书写的react组件最后都会变成 react.createElement(...)这一坨东西, // 转变前 export default ( ...
- React 16 源码瞎几把解读 【前戏】 为啥组件外面非得包个标签?
〇.看前准备 1.自行clone react最新代码 2.自行搭建一个能跑react的test项目 一.看表面:那些插件 如何解析JSX 有如下一段代码: // ---- hearder.jsx 组件 ...
- React Fiber源码分析 (介绍)
写了分析源码的文章后, 总觉得缺少了什么, 在这里补一个整体的总结,输出个人的理解~ 文章的系列标题为Fiber源码分析, 那么什么是Fiber,官方给出的解释是: React Fiber是对核心算法 ...
- 《React Native 精解与实战》书籍连载「React Native 源码学习方法及其他资源」
此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React Native 源码学习方法及其他资源. 最后的章节给大家介绍 React Native ...
- React的React.createContext()源码解析(四)
一.产生context原因 从父组件直接传值到孙子组件,而不必一层一层的通过props进行传值,相比较以前的那种传值更加的方便.简介. 二.context的两种实现方式 1.老版本(React16.x ...
- React的React.createElement源码解析(一)
一.什么是jsx jsx是语法糖 它是js和html的组合使用 二.为什么用jsx语法 高效定义模版,编译后使用 不会带来性能问题 三.jsx语法转化为js语法 jsx语法通过babel转化为 ...
- 【spring源码分析】IOC容器初始化(二)
前言:在[spring源码分析]IOC容器初始化(一)文末中已经提出loadBeanDefinitions(DefaultListableBeanFactory)的重要性,本文将以此为切入点继续分析. ...
随机推荐
- BZOJ 1923 外星千足虫(bitset优化线性基)
题意:给出m次n个千足虫的足数信息,确定在第几次测试后可以确定每个千足虫的来历. 我们可以观察到每个测试结果具有异或后依然成立的性质,于是实际上我们只需要从头到尾确定有n个线性相关的向量是在哪一个测试 ...
- 【bzoj1067】[SCOI2007]降雨量 倍增RMQ
题目描述 我们常常会说这样的话:“X年是自Y年以来降雨量最多的”.它的含义是X年的降雨量不超过Y年,且对于任意Y<Z<X,Z年的降雨量严格小于X年.例如2002,2003,2004和200 ...
- 【bzoj5157】[Tjoi2014]上升子序列 树状数组
题目描述 求一个数列本质不同的至少含有两个元素的上升子序列数目模10^9+7的结果. 题解 树状数组 傻逼题,离散化后直接使用树状数组统计即可.由于要求本质不同,因此一个数要减去它前一次出现时的贡献( ...
- [六]SpringBoot 之 连接数据库(mybatis)
在进行配置之前首先要了解springboot是如何使用纯java代码方式初始化一个bean的 以前的版本是在xml中使用beans标签,在其里面配置bean,那么纯Java代码怎么实现呢? 答案就是使 ...
- Linux内核设计与实现第四周读书笔记
第5章系统调用 5.1与内核通信 主要作用: 为用户控件提供了一种硬件的抽象接口. 保证了系统稳定性与安全性. 为用户空间&系统提供公共接口. 5.2API.POSIX和C库 一般情况,应用程 ...
- 【线段树】【CF19D】 Points
传送门 Description 在一个笛卡尔坐标系中,定义三种操作: \(add(x,y)\),将点\((x,y)\)标记在坐标系上 \(find(x,y)\),查询点\((x,y)\)严格右上方中, ...
- windows多线程接口介绍和使用
一windows多线程接口: 1 创建线程 CreateThread 与 _beginthreadex都可以实现创建线程,两个函数的参数 相同, HANDLEWINAPICreateThread( L ...
- 多重检验_LSD方法不准确性
sklearn实战-乳腺癌细胞数据挖掘(博客主亲自录制视频教程) https://study.163.com/course/introduction.htm?courseId=1005269003&a ...
- [DeeplearningAI笔记]卷积神经网络2.2经典网络
4.2深度卷积网络 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 [LeNet]--Lécun Y, Bottou L, Bengio Y, et al. Gradient-bas ...
- 782E. Underground Lab DFS 好题
Link 题意:给出一个图,有n个点,m条边,k个人,每个人至多只能走$\lceil\frac{2n}{k}\rceil$步,输出可行的方案即输出每个人所走的步数和所走点 思路: 由于保证给出的是连通 ...