控制台问题:
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';
import store from "./redux/store";
const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
// 检测redux 状态 发生变化 整个app 都调用 render() 作用是不用重复写 componentDidMount
store.subscribe( () => {
//不再创建 调用 createRoot 控制台就不报错了 程序正常运行 控制台输出也 ok 老师的旧版本可以 新版本就不行了
// const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
})
// If you want to start measuring performance in your app, pass a function
// to log results (for example: reportWebVitals(console.log))
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
处理方案:根本原因 版本跟老师的不同,处理方法也不同
调用subscribe 监控 时 不再创建虚拟dom就行
// const root = ReactDOM.createRoot(document.getElementById(‘root’));