ReactDom.render的详细解析


本文摘自PHP中文网,作者不言,侵删。

本篇文章给大家带来的内容是关于ReactDom.render的详细解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

步骤

1.创建ReactRoot

2.创建FiberRoot和FiberRoot

3.创建更新

render方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

render(

    element: React$Element<any>,

    container: DOMContainer,

    callback: ?Function,

  ) {

    invariant(

      isValidContainer(container),

      'Target container is not a DOM element.',

    );

    return legacyRenderSubtreeIntoContainer(

      null,

      element,

      container,

      false,

      callback,

    );

  },

render方法可以传入三个参数包括ReactElement、DOM的包裹节点,和渲染结束后执行的回调方法。
然后验证invariant验证container是否是有效的Dom节点。
最后返回legacyRenderSubtreeIntoContainer方法执行后的结果,再来看看这个方法的参数

1

2

3

4

5

6

7

function legacyRenderSubtreeIntoContainer(

  parentComponent: ?React$Component<any, any>,

  children: ReactNodeList,

  container: DOMContainer,

  forceHydrate: boolean,

  callback: ?Function,

)

这里传入五个参数,第一个是parentComponent不存在传入null,第二个是传入container的子元素,第三个是创建ReactRoot的包裹元素,第四个是协调更新的选项,第五个是渲染后的回调方法。

1

2

3

4

5

6

7

let root: Root = (container._reactRootContainer: any);

  if (!root) {

    // Initial mount

    root = container._reactRootContainer = legacyCreateRootFromDOMContainer(

      container,

      forceHydrate,

    );

先检验ReactRoot是否存在不存在则执行传入container,
forceHydrate后的legacyCreateRootFromDOMContainer函数创建一个ReactRoot。forceHydrate在render方法中传入的false,在Hydrate方法中传入的true,主要是为了区分服务端渲染和客户端渲染,true时未复用原来的节点适合服务端渲染,
如果是false则执行container.removeChild(rootSibling)删除所有的子节点。
然后返回通过 new ReactRoot(container, isConcurrent, shouldHydrate)

1

2

3

4

5

6

7

8

function ReactRoot(

  container: DOMContainer,

  isConcurrent: boolean,

  hydrate: boolean,

) {

  const root = createContainer(container, isConcurrent, hydrate);

  this._internalRoot = root;

}

在这个方法中调用createContainer创建root,这个方法从react-reconciler/inline.dom文件中引入:

1

2

3

4

5

6

7

export function createContainer(

  containerInfo: Container,

  isConcurrent: boolean,

  hydrate: boolean,

): OpaqueRoot {

  return createFiberRoot(containerInfo, isConcurrent, hydrate);

}

在这个方法中又调用了createFiberRoot方法创建FiberRoot
在创建玩root后执行unbatchedUpdates更新,传入root。render方法更新:

1

2

3

4

5

6

7

8

9

10

11

unbatchedUpdates(() => {

      if (parentComponent != null) {

        root.legacy_renderSubtreeIntoContainer(

          parentComponent,

          children,

          callback,

        );

      } else {

        root.render(children, callback);

      }

    });

执行updateContainer(children, root, null, work._onCommit);方法,这个方法最终调用enqueueUpdatescheduleWork,并返回expireTime,这些进行调度算法和进行优先级判断

【相关推荐:react视频教程】

以上就是ReactDom.render的详细解析的详细内容,更多文章请关注木庄网络博客

相关阅读 >>

javascript的console用法是什么

javascript怎么实现二维码

javascript中,nan是什么类型?

javascript数组方法有哪些

javascript中input怎么用

javascript中使用for...in 和object.keys()枚举对象属性的差异(附代码)

javascript是一种运行在什么语言

用什么软件写javascript代码

javascript的三种注释方式是什么

angularjs的ng-bind-html指令详解

更多相关阅读请进入《javascript》频道 >>




打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...