本篇文章给大家带来的内容是关于reactdom.render的详细解析,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。
步骤
1.创建reactroot
2.创建fiberroot和fiberroot
3.创建更新
render方法:
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方法执行后的结果,再来看看这个方法的参数
function legacyrendersubtreeintocontainer( parentcomponent: ?react$component<any, any>, children: reactnodelist, container: domcontainer, forcehydrate: boolean, callback: ?function,)
这里传入五个参数,第一个是parentcomponent不存在传入null,第二个是传入container的子元素,第三个是创建reactroot的包裹元素,第四个是协调更新的选项,第五个是渲染后的回调方法。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):function reactroot( container: domcontainer, isconcurrent: boolean, hydrate: boolean,) { const root = createcontainer(container, isconcurrent, hydrate); this._internalroot = root;}
在这个方法中调用createcontainer创建root,这个方法从react-reconciler/inline.dom文件中引入:
export function createcontainer( containerinfo: container, isconcurrent: boolean, hydrate: boolean,): opaqueroot { return createfiberroot(containerinfo, isconcurrent, hydrate);}
在这个方法中又调用了createfiberroot方法创建fiberroot
在创建玩root后执行unbatchedupdates更新,传入root。render方法更新:
unbatchedupdates(() => { if (parentcomponent != null) { root.legacy_rendersubtreeintocontainer( parentcomponent, children, callback, ); } else { root.render(children, callback); } });
执行updatecontainer(children, root, null, work._oncommit);方法,这个方法最终调用enqueueupdate和schedulework,并返回expiretime,这些进行调度算法和进行优先级判断
【相关推荐:react视频教程】
以上就是reactdom.render的详细解析的详细内容。