国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

React16源碼: React中的completeWork對(duì)HostText處理含更新的源碼實(shí)現(xiàn)

這篇具有很好參考價(jià)值的文章主要介紹了React16源碼: React中的completeWork對(duì)HostText處理含更新的源碼實(shí)現(xiàn)。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

HostText


1 )概述

  • completeWork 中 對(duì) HostText的處理
  • 在第一次掛載和后續(xù)更新的不同條件下進(jìn)行操作
    • 第一次掛載主要是創(chuàng)建實(shí)例
    • 后續(xù)更新其實(shí)也是重新創(chuàng)建實(shí)例

2 )源碼

定位到 packages/react-reconciler/src/ReactFiberCompleteWork.js#L663

case HostText 這里

case HostText: {
  let newText = newProps;
  // 符合這個(gè)條件,說明它不是第一次渲染,就是更新的狀態(tài)
  // 調(diào)用 updateHostText 進(jìn)行更新
  if (current && workInProgress.stateNode != null) {
    const oldText = current.memoizedProps;
    // If we have an alternate, that means this is an update and we need
    // to schedule a side-effect to do the updates.
    updateHostText(current, workInProgress, oldText, newText);
  } else {
    // 對(duì)于第一次渲染
    if (typeof newText !== 'string') {
      invariant(
        workInProgress.stateNode !== null,
        'We must have new props for new mounts. This error is likely ' +
          'caused by a bug in React. Please file an issue.',
      );
      // This can happen when we abort work.
    }
    // 跳過 context 處理
    const rootContainerInstance = getRootHostContainer();
    const currentHostContext = getHostContext();
    // 跳過 hydrate 處理
    let wasHydrated = popHydrationState(workInProgress);
    if (wasHydrated) {
      if (prepareToHydrateHostTextInstance(workInProgress)) {
        markUpdate(workInProgress);
      }
    } else {
      // 創(chuàng)建 文本 實(shí)例
      workInProgress.stateNode = createTextInstance(
        newText,
        rootContainerInstance,
        currentHostContext,
        workInProgress,
      );
    }
  }
  break;
}
  • 進(jìn)入 updateHostText

    updateHostText = function(
      current: Fiber,
      workInProgress: Fiber,
      oldText: string,
      newText: string,
    ) {
      // 這個(gè)非常簡單,通過前后 text 是否有區(qū)別
      // 如果不同,則創(chuàng)建新的 text實(shí)例
      if (oldText !== newText) {
        // If the text content differs, we'll create a new text instance for it.
        // 先跳過 context 的處理
        const rootContainerInstance = getRootHostContainer();
        const currentHostContext = getHostContext();
        workInProgress.stateNode = createTextInstance(
          newText,
          rootContainerInstance,
          currentHostContext,
          workInProgress,
        );
        // We'll have to mark it as having an effect, even though we won't use the effect for anything.
        // This lets the parents know that at least one of their children has changed.
        markUpdate(workInProgress);
      }
    };
    
    • 進(jìn)入 createTextInstance
      // packages/react-dom/src/client/ReactDOMHostConfig.js#L272
      export function createTextInstance(
        text: string,
        rootContainerInstance: Container,
        hostContext: HostContext,
        internalInstanceHandle: Object,
      ): TextInstance {
        if (__DEV__) {
          const hostContextDev = ((hostContext: any): HostContextDev);
          validateDOMNesting(null, text, hostContextDev.ancestorInfo);
        }
        const textNode: TextInstance = createTextNode(text, rootContainerInstance);
        // 這個(gè)方法之前閱過
        precacheFiberNode(internalInstanceHandle, textNode);
        return textNode;
      }
      
      • 進(jìn)入 createTextNode
        export function createTextNode(
          text: string,
          rootContainerElement: Element | Document,
        ): Text {
          // 最終 getOwnerDocumentFromRootContainer 這里返回一個(gè)dom對(duì)象,調(diào)用dom的 createTextNode 這個(gè)方法
          return getOwnerDocumentFromRootContainer(rootContainerElement).createTextNode(
            text,
          );
        }
        
        • 進(jìn)入 getOwnerDocumentFromRootContainer
        function getOwnerDocumentFromRootContainer(
          rootContainerElement: Element | Document,
        ): Document {
          // rootContainerElement.ownerDocument 這里是 window.document 對(duì)象
          // 這么做是為了兼容多平臺(tái)api的使用
          return rootContainerElement.nodeType === DOCUMENT_NODE
            ? (rootContainerElement: any)
            : rootContainerElement.ownerDocument;
        }
        
  • HostText 相比于 HostComponents 來說,它沒有多種選擇

  • 沒有各種各樣的屬性,整體來說非常的簡單

  • 一些特別說明的,寫在了上述代碼注釋中文章來源地址http://www.zghlxwxcb.cn/news/detail-821114.html

到了這里,關(guān)于React16源碼: React中的completeWork對(duì)HostText處理含更新的源碼實(shí)現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • React16源碼: React中的unwindWork的源碼實(shí)現(xiàn)

    React16源碼: React中的unwindWork的源碼實(shí)現(xiàn)

    unwindWork 1 )概述 在 renderRoot 的 throw Exception 里面, 對(duì)于被捕獲到錯(cuò)誤的組件進(jìn)行了一些處理 并且向上去尋找能夠處理這些異常的組件,比如說 class component 里面具有 getDerivedStateFromError 或者 componentDidCatch 這樣的生命周期方法 這個(gè)class component 就代表它可以處理它的子樹當(dāng)中渲

    2024年01月25日
    瀏覽(53)
  • React16源碼: React中的reconcileChildren的源碼實(shí)現(xiàn)

    reconcileChildren 1 )概述 在更新了一個(gè)節(jié)點(diǎn)之后,拿到它的props.children 要根據(jù)這個(gè)children里面的 ReactElement 來去創(chuàng)建子樹的所有的 fiber 對(duì)象 要根據(jù) props.children 來生成 fiber 子樹,然后判斷 fiber 對(duì)象它是否是可以復(fù)用的 因?yàn)槲覀冊(cè)诘谝淮武秩镜臅r(shí)候,就已經(jīng)渲染了整個(gè) fiber 子樹

    2024年01月20日
    瀏覽(25)
  • React16源碼: React中的updateClassComponent的源碼實(shí)現(xiàn)

    ClassComponent 的更新 1 ) 概述 在 react 中 class component,是一個(gè)非常重要的角色 它承擔(dān)了 react 中 更新整個(gè)應(yīng)用的API setState forceUpdate 在react當(dāng)中,只有更新了state之后,整個(gè)應(yīng)用才會(huì)重新進(jìn)行渲染 在 class component 中, 它的邏輯相對(duì)復(fù)雜 2 )源碼 在 packages/react-reconciler/src/ReactFiberB

    2024年01月21日
    瀏覽(21)
  • React16源碼: React中的renderRoot的源碼實(shí)現(xiàn)

    React16源碼: React中的renderRoot的源碼實(shí)現(xiàn)

    renderRoot 1 )概述 renderRoot 是一個(gè)非常復(fù)雜的方法 這個(gè)方法里處理很多各種各樣的邏輯, 它主要的工作內(nèi)容是什么? A. 它調(diào)用 workLoop 進(jìn)行循環(huán)單元更新 遍歷整個(gè) Fiber Tree,把每一個(gè)組件或者 dom 節(jié)點(diǎn)對(duì)應(yīng)的 Fiber 節(jié)點(diǎn)拿出來單一的進(jìn)行更新,這是一個(gè)循環(huán)的操作 把整棵 Fiber T

    2024年01月17日
    瀏覽(22)
  • React16源碼: React中的performWork的源碼實(shí)現(xiàn)

    performWork 1 )概述 performWork 涉及到在調(diào)度完成,或者同步任務(wù)進(jìn)來之后整個(gè) root 節(jié)點(diǎn)鏈條如何更新 怎么更新一棵 Fiber 樹,它的每一個(gè)節(jié)點(diǎn)是如何被遍歷到,以及如何進(jìn)行更新操作 A. 在執(zhí)行 performWork 時(shí)候,是否有 deadline 的區(qū)分 deadline 是通過 reactschedule 它的一個(gè)時(shí)間片,更新

    2024年01月17日
    瀏覽(20)
  • React16源碼: React中的beginWork的源碼實(shí)現(xiàn)

    beginWork 1 )概述 在 renderRoot 之后,要對(duì)我們的 Fiber 樹每一個(gè)節(jié)點(diǎn)進(jìn)行對(duì)應(yīng)的更新 更新節(jié)點(diǎn)的一個(gè)入口方法,就是 beginWork 這個(gè)入口方法會(huì)有幫助我們?nèi)?yōu)化整棵樹的更新過程 react 它的節(jié)點(diǎn)其實(shí)是非常多的,如果每一次子節(jié)點(diǎn)的一個(gè)更新 就需要每一個(gè)節(jié)點(diǎn)都執(zhí)行一遍更新的話

    2024年01月20日
    瀏覽(24)
  • React16源碼: React中的updateHostRoot的源碼實(shí)現(xiàn)

    HostRoot 的更新 1 )概述 HostRoot 是一個(gè)比較特殊的節(jié)點(diǎn), 因?yàn)樵谝粋€(gè)react應(yīng)用當(dāng)中 它只會(huì)有一個(gè) HostRoot , 它對(duì)應(yīng)的 Fiber 對(duì)象是我們的 RootFiber 對(duì)象 重點(diǎn)在于它的更新過程 2 )源碼 定位到 packages/react-reconciler/src/ReactFiberBeginWork.js#L612 HostRoot 創(chuàng)建更新的過程就是在 ReactFiberReconcile

    2024年01月22日
    瀏覽(29)
  • React16源碼: React中的IndeterminateComponent的源碼實(shí)現(xiàn)

    IndeterminateComponent 1 )概述 這是一個(gè)比較特殊的component的類型, 就是還沒有被指定類型的component 在一個(gè)fibrer被創(chuàng)建的時(shí)候,它的tag可能會(huì)是 IndeterminateComponent 在 packages/react-reconciler/src/ReactFiber.js 中,有一個(gè)方法 createFiberFromTypeAndProps 中,一開始就聲明了 在最終調(diào)用 createFibe

    2024年01月21日
    瀏覽(20)
  • React16源碼: React中的completeUnitOfWork的源碼實(shí)現(xiàn)

    React16源碼: React中的completeUnitOfWork的源碼實(shí)現(xiàn)

    completeUnitOfWork 1 )概述 各種不同類型組件的一個(gè)更新過程對(duì)應(yīng)的是在執(zhí)行 performUnitOfWork 里面的 beginWork 階段 它是去向下遍歷一棵 fiber 樹的一側(cè)的子節(jié)點(diǎn),然后遍歷到葉子節(jié)點(diǎn)為止,以及 return 自己 child 的這種方式 在 performUnitOfWork 里面,還有一個(gè)方法叫做 completeUnitOfWork 在

    2024年01月23日
    瀏覽(33)
  • React16源碼: React中的setState和forceUpdate源碼實(shí)現(xiàn)

    setState 和 forceUpdate 1 ) 概述 通過 class component 內(nèi)部的 setState ,以及 forceUpdate 去更新一個(gè)組件的過程 在react的應(yīng)用當(dāng)中,我們只有 ReactDOM.render setState ,以及 forceUpdate 這幾種種方式去更新react的應(yīng)用是合理的,其他沒有什么特別常用的方式去更新了 而且react官方推薦的也是用

    2024年01月25日
    瀏覽(22)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包