【react】react18由render替换为createRoot区别
发布日期:2021-06-28 21:54:49
浏览次数:2
分类:技术文章
本文共 2092 字,大约阅读时间需要 6 分钟。
前言
- 18版本由render替换为createRoot,看看有什么区别和为什么要这么做。
render与createRoot
- react18中装载了2个api,所以你用以前的也没事。在源码中都有导入:
import { findDOMNode, render, hydrate, unstable_renderSubtreeIntoContainer, unmountComponentAtNode,} from './ReactDOMLegacy';import { createRoot, isValidContainer} from './ReactDOMRoot';
-
名字很明显,一个叫legacy,在开发模式中,会提示你要换api了。
-
老版本是这么使用render 的:
import ReactDOM from 'react-dom';import App from 'App';const container = document.getElementById('app');// Initial render.ReactDOM.render(, container);// During an update, React would access// the root of the DOM element.ReactDOM.render( , container);
- 老版本渲染回调:
import ReactDOM from 'react-dom';import App from 'App';const container = document.getElementById('app');ReactDOM.render(container,, function() { // Called after inital render or any update. console.log('rendered').});
- 老版本注水:
import ReactDOM from 'react-dom';import App from 'App';const container = document.getElementById('app');// Render with hydration.ReactDOM.hydrate(, container);
- 新版本这么使用:
import ReactDOM from 'react-dom';import App from 'App';const container = document.getElementById('app');// Create a root.const root = ReactDOM.createRoot(container);// Initial render: Render an element to the root.root.render();// During an update, there's no need to pass the container again.root.render( );
- 新版本删除了渲染回调,因为回调触发在注水时可能与预期时间不一致。推荐使用idlecallback、settimeout或者ref,例如这么使用:
import ReactDOM from 'react-dom';function App({ callback }) { // Callback will be called when the div is first created. return ();}const rootElement = document.getElementById("root");const root = ReactDOM.createRoot(rootElement);root.render(Hello World
console.log("renderered")} />);
- 新版本注水:
import ReactDOM from ‘react-dom’;import App from 'App';const container = document.getElementById('app');// Create a root with hydration.const root = ReactDOM.createRoot(container, { hydrate: true });root.render();
-
可以看见新版本没有hydrate了。
-
更换api的原因有2个,第一个就是为了把hydrate这个api换掉,然后做成一个配置项。第二个就是没必要把container传递给render,也就是说没必要存储container。
转载地址:https://blog.csdn.net/yehuozhili/article/details/117912552 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
感谢大佬
[***.8.128.20]2024年04月05日 19时09分50秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
HashMap jdk1.7和1.8概述
2019-04-29
springboot多环境加载yml和logback配置
2019-04-29
几道简单的算法题(来自leetcode)
2019-04-29
推一波JAVA学习公众号
2019-04-29
org.apache.ibatis.reflection.ReflectionException: Error instantiating class with invalid types
2019-04-29
HttpsURLConnection 返回 400
2019-04-29
python基础语法05
2019-04-29
Linux基础01
2019-04-29
工作总结04-脚本
2019-04-29
01-MYSQL8环境搭建
2019-04-29
02-初始数据库
2019-04-29
原理解析!2021年教你增加拿到BAT等大厂offer几率,offer拿到手软
2019-04-29
原生安卓开发!驱动核心源码详解和Binder超系统学习资源,实战篇
2019-04-29
大牛手把手带你!2021年Android进阶者的新篇章,完整PDF
2019-04-29
太牛了!Android程序员最大的悲哀是什么?面试真题解析
2019-04-29
作为字节跳动面试官,BAT这种大厂履历意味着什么?赶快收藏备战金九银十!
2019-04-29
大牛深入讲解!Android面试中常问的MMAP到底是啥东东?再不刷题就晚了!
2019-04-29
太赞了!你会的还只有初级安卓工程师的技术吗?3面直接拿到offer
2019-04-29