React Fiber源碼分析 第一篇
React Fiber源碼分析 第二篇(同步模式)
React Fiber源碼分析 第三篇(異步狀態)
React Fiber源碼分析 第四篇(概括總結)node
React Fiber是React在V16版本中的大更新,利用了閒餘時間看了一些源碼,作個小記錄~react
先由babel編譯, 調用reactDOM.render,入參爲element, container, callback, 打印出來能夠看到element,container,callback分別表明着react元素、DOM原生元素,回調函數
bash
1.render實際上調用的是legacyRenderSubtreeIntoContainer函數babel
render: function (element, container, callback) {
return legacyRenderSubtreeIntoContainer(null, element, container, false, callback);
}
複製代碼
2.legacyRenderSubtreeIntoContainer 這個函數, 其實是初始化了root, 並調用了root.render方法, 而root是由legacyCreateRootFromDOMContainer函數返回的異步
function legacyRenderSubtreeIntoContainer(parentComponent, children, container, forceHydrate, callback) {
var root = container._reactRootContainer;
if (!root) {
// 初始化root
root = container._reactRootContainer = legacyCreateRootFromDOMContainer(container, forceHydrate);// Initial mount should not be batched.
unbatchedUpdates(function () {
if (parentComponent != null) {
root.legacy_renderSubtreeIntoContainer(parentComponent, children, callback);
} else {
// 調用root的render方法
root.render(children, callback);
}
});
} else {
......
}
}
複製代碼
3.從代碼中看出, legacyCreateRootFromDOMContainer執行了兩個操做, 一個是清除掉全部的子元素, 另一個則是返回了一個 ReactRoot實例, 這裏須要注意一點, root默認是同步更新的, 即isAsync 默認爲false函數
function legacyCreateRootFromDOMContainer(container, forceHydrate) {
...// 清除全部子元素
if (!shouldHydrate) {
var warned = false;
var rootSibling = void 0;
while (rootSibling = container.lastChild) {
{
if (!warned && rootSibling.nodeType === ELEMENT_NODE && rootSibling.hasAttribute(ROOT_ATTRIBUTE_NAME)) {
warned = true;
}
}
container.removeChild(rootSibling);
}
}// 默認爲同步狀態
var isAsync = false;
return new ReactRoot(container, isAsync, shouldHydrate);
}
複製代碼
4.從ReactRoot中, 咱們把createContainer返回值賦給了 實例的**_internalRoot**, 往下看createContainer源碼分析
function ReactRoot(container, isAsync, hydrate) {
var root = createContainer(container, isAsync, hydrate);
this._internalRoot = root;
}
複製代碼
5.從createContainer看出, createContainer其實是直接返回了createFiberRoot, 而createFiberRoot則是經過createHostRootFiber函數的返回值uninitializedFiber,並將其賦值在root對象的current上, 這裏須要注意一個點就是,uninitializedFiber的stateNode的值是root, 即他們互相引用post
function createContainer(containerInfo, isAsync, hydrate) {
return createFiberRoot(containerInfo, isAsync, hydrate);
}
function createFiberRoot(containerInfo, isAsync, hydrate) {
// 建立hostRoot並賦值給uninitiallizedFiber
var uninitializedFiber = createHostRootFiber(isAsync);
// 互相引用
var root = void 0;
root = {
current: uninitializedFiber,
...
};
uninitializedFiber.stateNode = root;
複製代碼
6.最後是返回了一個fiberNode的實例, 在這裏咱們能夠看到mode這個字段, 因爲在一開始就將isAsync初始化爲false, 因此mode實際上就表明了同步ui
在這裏, 整理一下各個實例的關係,this
root爲ReactRoot實例,
root._internalRoot 即爲fiberRoot實例,
root._internalRoot.current即爲Fiber實例,
root._internalRoot.current.stateNode = root._internalRoot
function createHostRootFiber(isAsync) {
var mode = isAsync ? AsyncMode | StrictMode : NoContext;
return createFiber(HostRoot, null, null, mode);
}
var createFiber = function (tag, pendingProps, key, mode) {
return new FiberNode(tag, pendingProps, key, mode);
};
function FiberNode(tag, pendingProps, key, mode) {
// 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;
...
}
複製代碼
7.初始化完成, 接下來就是root.render執行了, 在這裏, 先暫時忽略ReactWork, 把work._onCommit當成一個回調函數便可, 能夠看到, root即FiberRoot實例被當成參數傳入了updateContsainer裏面, 往下看updateContainer
ReactRoot.prototype.render = function (children, callback) {
var root = this._internalRoot;
var work = new ReactWork();
callback = callback === undefined ? null : callback;
if (callback !== null) {
work.then(callback);
}
updateContainer(children, root, null, work._onCommit);
return work;
};
複製代碼
8.updateContsainer裏面使用了 currentTime 和 expirationTime,
currentTime是用來計算expirationTime,
expirationTime表明着優先級, 留在後續分析,
這裏咱們知道是同步更新 即 expirationTime = 1. 緊接着調用了updateContainerAtExpirationTime
function updateContainer(element, container, parentComponent, callback) {
var current$$1 = container.current;
var currentTime = requestCurrentTime();
var expirationTime = computeExpirationForFiber(currentTime, current$$1);
return updateContainerAtExpirationTime(element, container, parentComponent, expirationTime, callback);
}
複製代碼
9.updateContainerAtExpirationTime將current(即Fiber實例)提取出來, 並做爲參數傳入調用scheduleRootUpdate
function updateContainerAtExpirationTime(element, container, parentComponent, expirationTime, callback) {
// TODO: If this is a nested container, this won't be the root. var current$$1 = container.current; ... return scheduleRootUpdate(current$$1, element, expirationTime, callback); } 複製代碼
到了這裏告一段落, scheduleRootUpdate接下來就是React新版本異步渲染的核心了, 留在下一篇繼續解讀