[英]React App: Cannot find module 'react-dom/client' or its corresponding type declarations
I'm currently experiencing this error and I'm not really sure how to fix it.我目前遇到此错误,我不确定如何修复它。 I've been trying to merge a project with components stored in bit.dev.
我一直在尝试将项目与存储在 bit.dev 中的组件合并。
import React from 'react';
import { createRoot } from 'react-dom/client'; // Cannot find module 'react-dom/client' or its corresponding type declarations.
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
const rootElement = document.getElementById('root');
if (!rootElement) throw new Error('Failed to find the root element');
const root = createRoot(rootElement);
root.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>
);
Here is the code.这是代码。 Thanks for your help!
谢谢你的帮助!
For anyone that might experience the same problem, the version of react bit.dev is using and the version the actual app is using are different.对于可能遇到相同问题的任何人,react bit.dev 使用的版本与实际应用程序使用的版本不同。 All I had to do was change the way the app was rendered (in this case react v17).
我所要做的就是更改应用程序的呈现方式(在本例中为 React v17)。
import React from 'react';
import ReactDOM from 'react-dom';
import './index.css';
import App from './App';
import reportWebVitals from './reportWebVitals';
import { BrowserRouter } from 'react-router-dom';
import 'bootstrap/dist/css/bootstrap.min.css';
ReactDOM.render(
<React.StrictMode>
<BrowserRouter>
<App />
</BrowserRouter>
</React.StrictMode>,
document.getElementById('root')
);
After running npm install react react-dom
or yarn add react react-dom
as instructedhere , you should then run按照此处的说明运行
npm install react react-dom
或yarn add react react-dom
后,您应该运行
npm install -D @types/react-dom
or或者
yarn add -D @types/react-dom
This will add react-dom/client
type declarations to your project and will remove the error you pointed at on line 2 of your code.这会将
react-dom/client
类型声明添加到您的项目中,并将删除您在代码第 2 行指出的错误。
Try尝试
import * as ReactDOM from 'react-dom/client';
Then ReactDOM.createRoot
然后
ReactDOM.createRoot
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.