【转载】解决关于升级 React 18的报错问题

转自: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(
        
          
        
      );
点赞

发表回复

电子邮件地址不会被公开。必填项已用 * 标注