〇、先来看看常用的常量

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的更多相关文章

  1. React 16 源码瞎几把解读 【二】 react组件的解析过程

    一.一个真正的react组件编译后长啥样? 我们瞎几把解读了react 虚拟dom对象是怎么生成的,生成了一个什么样的解构.一个react组件不光由若干个这些嵌套的虚拟dom对象组成,还包括各种生命周 ...

  2. React 16 源码瞎几把解读 【三 点 一】 把react组件对象弄到dom中去(矛头指向fiber,fiber不解读这个过程也不知道)

    一.ReactDOM.render 都干啥了 我们在写react的时候,最后一步肯定是 ReactDOM.render( <div> <Home name="home&qu ...

  3. React 16 源码瞎几把解读 【一】 从jsx到一个react 虚拟dom对象

    一.jsx变createElement 每一个用jsx语法书写的react组件最后都会变成 react.createElement(...)这一坨东西, // 转变前 export default ( ...

  4. React 16 源码瞎几把解读 【前戏】 为啥组件外面非得包个标签?

    〇.看前准备 1.自行clone react最新代码 2.自行搭建一个能跑react的test项目 一.看表面:那些插件 如何解析JSX 有如下一段代码: // ---- hearder.jsx 组件 ...

  5. React Fiber源码分析 (介绍)

    写了分析源码的文章后, 总觉得缺少了什么, 在这里补一个整体的总结,输出个人的理解~ 文章的系列标题为Fiber源码分析, 那么什么是Fiber,官方给出的解释是: React Fiber是对核心算法 ...

  6. 《React Native 精解与实战》书籍连载「React Native 源码学习方法及其他资源」

    此系列文章将整合我的 React 视频教程与 React Native 书籍中的精华部分,给大家介绍 React Native 源码学习方法及其他资源. 最后的章节给大家介绍 React Native ...

  7. React的React.createContext()源码解析(四)

    一.产生context原因 从父组件直接传值到孙子组件,而不必一层一层的通过props进行传值,相比较以前的那种传值更加的方便.简介. 二.context的两种实现方式 1.老版本(React16.x ...

  8. React的React.createElement源码解析(一)

    一.什么是jsx  jsx是语法糖  它是js和html的组合使用  二.为什么用jsx语法 高效定义模版,编译后使用 不会带来性能问题 三.jsx语法转化为js语法  jsx语法通过babel转化为 ...

  9. 【spring源码分析】IOC容器初始化(二)

    前言:在[spring源码分析]IOC容器初始化(一)文末中已经提出loadBeanDefinitions(DefaultListableBeanFactory)的重要性,本文将以此为切入点继续分析. ...

随机推荐

  1. xpath定位相邻元素方法

    在定位页面元素时,有时候需要根据某个元素特征,去定位其相邻元素/兄弟元素,或者定位其父元素的兄弟元素(或叔伯元素的子元素).这里引入xpath的两个定位方法: preceding-sibling fo ...

  2. Elasticsearch在windows上安装好了之后怎么使用?

    windows 10上安装Elasticsearch过程记录 一.安装和配置Java JDK1.下载:http://download.oracle.com/otn ... 4.exe2.设置环境变量: ...

  3. 【bzoj3295】动态逆序对

    Portal --> bzoj3295 Solution 虽然说这个可能原本是一道愉快的树套树但是 ​ 没有强制在线并且是三维限制那就大力cdq分治啊! ​ 看到"按照某个顺序依次删除 ...

  4. pxp

    Time Limit: 2000 ms Memory Limit: 512 MB Description 给定 \(n\), 求\(\sum\limits_{p,q∈primes}[pq≤n]\) ( ...

  5. move_base代码学习一

    System overview move_base 源码 API nav_core BaseGlobalPlanner BaseLocalPlanner RecoveryBehavior Recove ...

  6. SSH 阿里云服务器

    1.在服务机上操作 创建要远程登录的用户和密码 sudo adduser username    正在添加用户“username”... 正在添加新组“username”(1001)... 正在添加新 ...

  7. Codeforces 895.B XK Segments

    B. XK Segments time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  8. C++并发编程 等待与唤醒

    C++并发编程 等待与唤醒 条件变量 条件变量, 包括(std::condition_variable 和 std::condition_variable_any) 定义在 condition_var ...

  9. Redis 为什么用跳表而不用平衡树

    Redis 为什么用跳表而不用平衡树? 本文是<Redis内部数据结构详解>系列的第六篇.在本文中,我们围绕一个Redis的内部数据结构--skiplist展开讨论. Redis里面使用s ...

  10. 主席树 或者 离散化+分块 BZOJ 4636

    4636: 蒟蒻的数列 Time Limit: 30 Sec  Memory Limit: 256 MBSubmit: 381  Solved: 177[Submit][Status][Discuss ...