我将 React 更新到 v18,每当热模块替换触发并注入新的 javascript 代码时,我的 Webpack 开发服务器都会出现控制台错误:
Warning: You are calling ReactDOMClient.createRoot() on a container that has already been passed to createRoot() before. Instead, call root. render() on the existing root instead if you want to update it.
index.js文件
import React from 'react';
import ReactDOM from 'react-dom/client';
import App from './App.jsx';
const container = document.getElementById('root');
const root = ReactDOM.createRoot(container);
root.render(<App />);
if (module.hot) module.hot.accept(function (err) {
console.log('An error occurred while accepting new version');
});
Run Code Online (Sandbox Code Playgroud)
webpack.config.js
const path = …Run Code Online (Sandbox Code Playgroud)