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)的重要性,本文将以此为切入点继续分析. ...
随机推荐
- 第120天:移动端-Bootstrap基本使用方法
一.Bootstrap使用 1.搭建Bootstrap页面骨架及项目目录结构 ``` ├─ /weijinsuo/ ··················· 项目所在目录 └─┬─ /css/ ···· ...
- 打印实例对象的名字 默认调用父类的toString 可重写
- Contest 5
A:这我怎么没学傻了啊.整个一傻逼题一眼容斥我连暴力都写不出来啊.显然序列是没有什么用的,考虑求众数小于x的概率,显然可以枚举有几个超过容斥一发.虽然要算的组合数非常大,发现可以抵消很大一部分,最后算 ...
- App简介及登录页面
一. APP目录 app目录: -migrations 数据操作记录,是自动创建的.数据修改表结构 -__init__.py #在python3里面可有可无都行 -__init__.py -admin ...
- ans menu list
ans menu list 1. 系统配置 a) 基本设置 i. NTP ii. 配置模式 iii. 主机信息 b) 高可用性 i. 节点 ii. 路由监视器 iii. 故障转移接口群 c) 设备标识 ...
- 【BZOJ3625/CF438E】小朋友和二叉树(多项式求逆,多项式开方)
[BZOJ3625/CF438E]小朋友和二叉树(多项式求逆,多项式开方) 题面 BZOJ CodeForces 大致题意: 对于每个数出现的次数对应的多项式\(A(x)\) 求\[f(x)=\fra ...
- <深入理解计算机系统>第七章读书笔记
第七章读书笔记 链接 链接:将各种代码和数据部分收集起来并组合成为一个单一文件的过程.(这个文件可被加载或拷贝到存储器并执行) 链接可以执行于编译,加载或运行时. 静态链接: 两个主要任务: 1 符号 ...
- 什么是end-to-end神经网络?
https://www.zhihu.com/question/51435499 来源:知乎著作权归作者所有. 国立台湾大学的李宏毅教授在其机器学习课程中有讲到深度神经网络的 End-to-end Le ...
- [USACO09OPEN] 工作调度Work Scheduling (贪心/堆)
[USACO09OPEN] 工作调度Work Scheduling 题意翻译 约翰有太多的工作要做.为了让农场高效运转,他必须靠他的工作赚钱,每项工作花一个单位时间. 他的工作日从0时刻开始,有10^ ...
- libevent学习笔记(参考libevent深度剖析)
最近自学libevent事件驱动库,参考的资料为libevent2.2版本以及张亮提供的<Libevent源码深度剖析>, 参考资料: http://blog.csdn.net/spark ...