这篇文章给大家分享的是有关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,这些进行调度算法和进行优先级判断
感谢各位的阅读!关于ReactDom.render指的是什么就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到吧!
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。