转自:https://www.cnblogs.com/maxiaocang/p/16087320.html
当使用 React 18 版本构建项目时,在运行项目时会在控制台看到如下报错信息:
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
解决方法
修改index.js文件
- 修改ReactDom的引入路径为'react-dom/client'
- 修改render函数中ReactDom.render() 方法为:
ReactDOM.createRoot(document.getElementById('root')) .render( );
Google Chrome
Windows 10