关于React18控制台报错:ReactDOM.render is no longer supported in React 18 Use createRoot instead. Until.....

React团队在3月29日新推出了React v18.0版本,现在npm 默认的就是18版本,由于React 18 不再支持 ReactDOM.render。控制台会抛出:

 

Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until you switch to the new API, your app will behave as if it's running React 17. 的警告信息,虽然不会影响程序正常的运行,但是控制台一直有警告强迫症属实难受。其实在index.js入口文件中改用 createRoot即可消除警告。 

 

import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';

import { createRoot } from 'react-dom/client';
const container = document.getElementById('root');
const root = createRoot(container);
root.render(<App />);


// 被注释的是之前ReactDOM.render的代码
// ReactDOM.render(
//   <React.StrictMode>
//     <App />
//   </React.StrictMode>,
//   document.getElementById('root')
// );

然后控制台就干干净净了

 

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值