报错Warning: ReactDOM.render is no longer supported in React 18.Use createRoot instead.Until you...

报错不影响正常渲染页面,报错图示:

 翻译如下:

反应域。React 18中不再支持渲染。改用createRoot。在你切换到新的API之前,你的应用程序将表现得像运行React 17。了解更多信息:https://reactjs.org/link/switch-to-createroot

点击后面的链接,可以看到下图:

 把里面的文字翻译如下:

React 18 introduces a new root API which provides better ergonomics for managing roots. The new root API also enables the new concurrent renderer, which allows you to opt-into concurrent features.

React 18引入了一种新的根API,该API为管理根提供了更好的人体工程学。新的根API还支持新的并发渲染器,允许您选择并发功能。

在下面还有代码:

// Before
import { render } from 'react-dom';
const container = document.getElementById('app');
render(<App tab="home" />, container);

// After
import { createRoot } from 'react-dom/client';
const container = document.getElementById('app');
const root = createRoot(container); // createRoot(container!) if you use TypeScript
root.render(<App tab="home" />);

根据以上内容,可以这样子解决这个报错

更改前入口文件代码:

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

ReactDOM.render(
    <Router />, document.getElementById('root')
)

更改后index.js代码如下:

import { createRoot } from 'react-dom/client';
import App from '../App'

//第一种写法
const container = document.getElementById('root');
const root = createRoot(container); 
root.render( <App />);
//第二种写法
createRoot(document.getElementById('root')).render( <App />)
  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

super码力

么么哒,夏天来块儿冰西瓜!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值