简体   繁体   English

React App:找不到模块“react-dom/client”或其相应的类型声明

[英]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-domyarn 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.

相关问题 React:找不到模块或其相应的类型 declarations.ts - React: Cannot find module or its corresponding type declarations.ts React Native - 找不到模块 X 或其相应的类型声明 - React Native - Cannot find module X or its corresponding type declarations 找不到模块“react-lazily”或其相应的类型声明 - Cannot find module 'react-lazily' or its corresponding type declarations 找不到模块“反应”或其相应的类型声明 - Cannot find module 'react' or its corresponding type declarations 未捕获的错误:找不到模块“react-dom/client” - Uncaught Error: Cannot find module 'react-dom/client' 错误:找不到模块“react-dom” - Error: Cannot find module “react-dom” 找不到模块&#39;file-name.png&#39;或其相应的类型声明 - 打字稿反应 - Cannot find module 'file-name.png' or its corresponding type declarations - typescript react ReScript,TypeScript:找不到模块“@rescript/react/src/ReactDOM.gen”或其相应的类型声明 - ReScript, TypeScript: Cannot find module '@rescript/react/src/ReactDOM.gen' or its corresponding type declarations React,Storybook - TS2307:找不到模块“按钮”或其相应的类型声明 CAN SB RESOLVE? - React,Storybook - TS2307: Cannot find module 'Button' or its corresponding type declarations CAN SB RESOLVE? 找不到模块“@react-navigation/native”或其相应的类型声明 - Cannot find module '@react-navigation/native' or its corresponding type declarations
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM