错误:目标容器不是 React 的 DOM 元素
我已经制作了此React应用程序,但我一直遇到此错误:
未被发现的错误:createroot(...):目标容器不是DOM元素。
有人可以帮我吗?这是我的index.html和index.js。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
ReactDOM.createRoot(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
我已经尝试了上传到Internet的所有可能解决方案,但不幸的是,它们尚未起作用。
I've made this React app but i keep getting this error:
Uncaught Error: createRoot(...): Target container is not a DOM element.
Can someone help me please? This is my index.html and index.js.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="icon" href="%PUBLIC_URL%/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="theme-color" content="#000000" />
<meta
name="description"
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
<title>React App</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
<div id="root"></div>
</body>
</html>
import React from "react";
import ReactDOM from "react-dom/client";
import App from "./App";
import reportWebVitals from "./reportWebVitals";
ReactDOM.createRoot(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById("root")
);
reportWebVitals();
I've already tried every possible solutions that are uploaded to the internet, but unfortunately non of them hasn't worked yet.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
作为 docs say ,签名是
:
因此,您需要将代码更改为
As the docs say, the signature is:
And it replaces:
So you need to change your code to
我遇到了同样的错误,此页面帮助我修复了它:
https://www.sharooq.com/solved -createroot-target-container-is-not-a-dom-element
可能您对 document.getElementById("root") 的调用发生在页面完成 DOM 设置之前。尝试将您的代码移至:
I had this same error, and this page helped me fix it:
https://www.sharooq.com/solved-createroot-target-container-is-not-a-dom-element
Probably your call to document.getElementById("root") is happening before the page has finished setting up the DOM. Try moving your code into:
从反应文档, https://17.reactjs.org/docs /concurrent-mode-reference.html#createroot
代码行使用此代码
这应该可以修复错误。
from the react documentation, https://17.reactjs.org/docs/concurrent-mode-reference.html#createroot
line of your code with this code
This should fix the error.
我遇到了这个问题,这是出于愚蠢的原因。我意外地交换了
index.js
,因此它在创建我的div id =“ root”
之前就运行了。因此,目前React正在寻找DOM确实不存在。说明性示例:
I ran into this problem for an extremely silly reason. I accidentally swapped
index.js
around so it ran before mydiv id="root"
was created. So the DOM truly didn't exist at the moment react was looking for it.Illustrative examples:
对我来说,问题是 dom 遍历触发得太早,以至于无法正确找到该元素。
使用
defer
加载脚本为我解决了这个问题:您可以了解更多关于为什么这通常也是一个好主意此处。
For me the issue was, that the dom traversal was triggered too early, such that it could not find that element correctly.
Loading the script with
defer
fixed it for me:You can learn more on why this also generally a good idea here.
我遇到了同样的问题,但我也在
index.html
文件中包含了标记。我设法使用解决方案在此问题中解决了我的问题。通过 Webpack 提供我的应用程序时我也遇到了这个问题5 的 webpack-dev-server 所以我很想知道你的使用和/或配置为您的应用程序提供服务,看看这是否不是 JS 中的问题。
I had the same issue but I included the
<script>
tag in theindex.html
file as well. I managed to resolve my issue using the solution in this SO question. I also ran into this issue when serving my app via Webpack 5's webpack-dev-server so I'd be curious to know what your serving your application with and/or the configuration to see if this perhaps isn't an issue in the JS.使用最新的 React 版本,您可以按如下方式渲染组件:
With the latest React version, you can render your component as follows:
我遇到了同样的问题,我通过在webpack.config.js中添加模板配置来修复它。
I had the same issue and i had fixed it by adding template configuration in HtmlWebpackPlugin in webpack.config.js
我由于不同的原因得到了同样的错误。
就我而言,index.html 中的 div id 与 index.js 中指定的 id 不匹配。
索引.html:
索引.js:
I got the same error for a different reason.
In my case, there was a mismatch between the id of the div in my index.html and the id specified in my index.js.
index.html:
index.js: