报错不影响正常渲染页面,报错图示:
翻译如下:
反应域。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 />)