亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

React Fiber結(jié)構(gòu)的創(chuàng)建步驟

 更新時間:2021年04月15日 10:04:40   作者:光光同學22167  
這篇文章主要介紹了React Fiber結(jié)構(gòu)的創(chuàng)建步驟,幫助大家更好的理解和學習使用React,感興趣的朋友可以了解下

React Fiber的創(chuàng)建

當前React版本基于V17.0.2版本,本篇主要介紹fiber結(jié)構(gòu)的創(chuàng)建。

一、開始之前

個人理解,如有不對,請指出。

首先需要配置好React的debugger開發(fā)環(huán)境,入口在這里:github

執(zhí)行npm run i,安裝依賴,npm start運行環(huán)境。

二、從React.render開始

通過在項目入口處調(diào)用React.render,打上Debug,查看React調(diào)用棧。

const root = document.getElementById('root');
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  root
);

在React調(diào)用render之后,在傳入基礎(chǔ)的配置后,調(diào)用legacyRenderSubtreeIntoContainer。

export function render(
  element: React$Element<any>,
  container: Container,
  callback: ?Function,
) {
  // 刪除一些環(huán)境代碼
  // ...
  return legacyRenderSubtreeIntoContainer(
    null,
    element,
    container,
    false,
    callback,
  );
}

在React調(diào)用render之后,在傳入基礎(chǔ)的配置后,調(diào)用legacyRenderSubtreeIntoContainer。

export function render(
  element: React$Element<any>,
  container: Container,
  callback: ?Function,
) {
  // 刪除一些環(huán)境代碼
  // ...
  return legacyRenderSubtreeIntoContainer(
    null,
    element,
    container,
    false,
    callback,
  );
}

legacyRenderSubtreeIntoContainer一共做了兩件事情,一個是生成了fiberRoot,一個是調(diào)用updateContainer。

進入legacyCreateRootFromDOMContainer函數(shù),查看如何生成fiberRoot。 在函數(shù)內(nèi)部,調(diào)用了createLegacyRoot,在這里區(qū)分了下,是否使用hydrate,如下:

  return createLegacyRoot(
    container,
    shouldHydrate
      ? {
        hydrate: true,
      }
      : undefined,
  );

對于createLegacyRoot來說,是用來實例化ReactDOMLegacyRoot函數(shù)的,通過后續(xù)調(diào)用,終于進入到root的生成,調(diào)用createRootImpl函數(shù),實例化root。

進入createFiberRoot函數(shù),初始化FiberRootNode。

function FiberRootNode(containerInfo, tag, hydrate) {
  this.tag = tag; // 類型
  this.containerInfo = containerInfo; // container
  this.pendingChildren = null; 
  this.current = null;
  this.pingCache = null;
  this.finishedWork = null;
  this.timeoutHandle = noTimeout;
  this.context = null;
  this.pendingContext = null;
  this.hydrate = hydrate;
  this.callbackNode = null;
  this.callbackPriority = NoLanePriority;
  this.eventTimes = createLaneMap(NoLanes);
  this.expirationTimes = createLaneMap(NoTimestamp);

  this.pendingLanes = NoLanes;
  this.suspendedLanes = NoLanes;
  this.pingedLanes = NoLanes;
  this.mutableReadLanes = NoLanes;
  this.finishedLanes = NoLanes;

  this.entangledLanes = NoLanes;
  this.entanglements = createLaneMap(NoLanes);

  // ....


}

這里的tag,有以下幾種類型。

export type RootTag = 0 | 1;

上述的結(jié)構(gòu)是fiberRootNode節(jié)點。

rootTag 等于0 時,代表legacy渲染模式,等于1時,代表Concurrent mode渲染,也就是說,傳統(tǒng)我們使用React.render進行渲染,當調(diào)用React.createRoot時,進入Concurrent mode渲染模式,即并行渲染。

現(xiàn)在我們一起看看fiber的結(jié)構(gòu)。

  const uninitializedFiber = createHostRootFiber(tag, strictModeLevelOverride);
  root.current = uninitializedFiber;
  uninitializedFiber.stateNode = root;

uninitializedFiber為創(chuàng)建的FiberNode的創(chuàng)建的實例。

const createFiber = function(
  tag: WorkTag,
  pendingProps: mixed,
  key: null | string,
  mode: TypeOfMode,
): Fiber {
  // $FlowFixMe: the shapes are exact here but Flow doesn't like constructors
  return new FiberNode(tag, pendingProps, key, mode);
};

通過基礎(chǔ)的創(chuàng)建,生成FiberNode結(jié)構(gòu),如下

function FiberNode(
  tag: WorkTag,
  pendingProps: mixed,
  key: null | string,
  mode: TypeOfMode,
) {
  // Instance
  this.tag = tag;//組件類型
  this.key = key;//key屬性
  this.elementType = null;//元素類型,類函數(shù),顯示類,div顯示div
  this.type = null;//func或者class
  this.stateNode = null;//dom節(jié)點

  // Fiber
  this.return = null;//指向父節(jié)點
  this.child = null;//指向子節(jié)點
  this.sibling = null;//兄弟節(jié)點
  this.index = 0;//

  this.ref = null;

  this.pendingProps = pendingProps;//等待中的屬性pendingProps
  this.memoizedProps = null; //記憶屬性,一般存放props
  this.updateQueue = null;//更新隊列
  this.memoizedState = null;// 一般存放state
  this.dependencies = null;

  this.mode = mode;

  // Effects相關(guān)
  this.flags = NoFlags;
  this.subtreeFlags = NoFlags;
  this.deletions = null;

  this.lanes = NoLanes;
  this.childLanes = NoLanes;

  this.alternate = null;//指向workInProgress
}

 FiberNode基本顯示如上,elementType和type的基礎(chǔ)類型為function、class。

通過對比fiberRootNode結(jié)構(gòu),和下面的代碼,生成最終的FiberNode 結(jié)構(gòu)。

render() {
    const { name, count } = this.state;
    return (
      <div className="App">
          <Button name={name} />
        {
          count
        }
      </div>
    );
  }
ReactDOM.render(
  <React.StrictMode>
    <App />
  </React.StrictMode>,
  root
);

通過最后執(zhí)行,生成fiberRoot鏈表結(jié)構(gòu)。

最后,調(diào)用unbatchedUpdates,進行渲染。

進入updateContainer函數(shù)。

unbatchedUpdates(() => {
  // 更新container
  updateContainer(children, fiberRoot, parentComponent, callback);
});

三、結(jié)束

以上就是React Fiber結(jié)構(gòu)的創(chuàng)建步驟的詳細內(nèi)容,更多關(guān)于React Fiber結(jié)構(gòu)的創(chuàng)建的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • React?antd中setFieldsValu的簡便使用示例代碼

    React?antd中setFieldsValu的簡便使用示例代碼

    form.setFieldsValue是antd?Form組件中的一個方法,用于動態(tài)設(shè)置表單字段的值,它接受一個對象作為參數(shù),對象的鍵是表單字段的名稱,值是要設(shè)置的字段值,這篇文章主要介紹了React?antd中setFieldsValu的簡便使用,需要的朋友可以參考下
    2023-08-08
  • react-router JS 控制路由跳轉(zhuǎn)實例

    react-router JS 控制路由跳轉(zhuǎn)實例

    這篇文章主要介紹了react-router JS 控制路由跳轉(zhuǎn)實例,react實現(xiàn)路由可以直接使用react-router。有興趣的可以了解一下
    2017-06-06
  • create-react-app 修改為多入口編譯的方法

    create-react-app 修改為多入口編譯的方法

    這篇文章主要介紹了create-react-app 修改為多入口編譯的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • React利用scheduler思想實現(xiàn)任務(wù)的打斷與恢復

    React利用scheduler思想實現(xiàn)任務(wù)的打斷與恢復

    這篇文章主要為大家詳細介紹了React如何利用scheduler思想實現(xiàn)任務(wù)的打斷與恢復,文中的示例代碼講解詳細,感興趣的小伙伴可以參考一下
    2024-03-03
  • React中嵌套組件與被嵌套組件的通信過程

    React中嵌套組件與被嵌套組件的通信過程

    這篇文章主要介紹了React中嵌套組件與被嵌套組件的通信過程,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2018-07-07
  • React render核心階段深入探究穿插scheduler與reconciler

    React render核心階段深入探究穿插scheduler與reconciler

    這篇文章主要介紹了React render核心階段穿插scheduler與reconciler,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2022-11-11
  • React組件化的一些額外知識點補充

    React組件化的一些額外知識點補充

    React是一個用于構(gòu)建用戶界面的JavaScript庫,下面這篇文章主要給大家介紹了關(guān)于React組件化的一些額外知識點,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-10-10
  • React-Hooks之useImperativeHandler使用介紹

    React-Hooks之useImperativeHandler使用介紹

    這篇文章主要為大家介紹了React-Hooks之useImperativeHandler使用介紹,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-07-07
  • JS中使用react-tooltip插件實現(xiàn)鼠標懸浮顯示框

    JS中使用react-tooltip插件實現(xiàn)鼠標懸浮顯示框

    前段時間遇到的一個需求,要求鼠標懸停顯示使用描述, 用到了react-tooltip插件,今天寫一個總結(jié),感興趣的朋友跟隨小編一起看看吧
    2019-05-05
  • Objects are not valid as a React child報錯解決

    Objects are not valid as a Rea

    這篇文章主要為大家介紹了Objects are not valid as a React child報錯解決方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-12-12

最新評論