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

React报错之Warning: ReactDOM.render is no longer supported in React 18. Use createRoot instead. Until y,第1张

1.问题描述:在编写React执行之后发现控制台报了这个错误

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. Learn more: https://reactjs.org/link/switch-to-createroot

2.原因:

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

3.解决方法:
import React from "react";
import App from "./App";
import { ReactDOM } from "react";
import {createRoot} from 'react-dom/client' 

const container=document.getElementById('root');
const root=createRoot(container);
root.render(<App/>);

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1321564.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-12
下一篇 2022-06-12

发表评论

登录后才能评论

评论列表(0条)

保存