“React error in index.js, ‘Uncaught ReferenceError: dom is not defined'”

huangapple go评论73阅读模式
英文:

React error in index.js, "Uncaught ReferenceError: dom is not defined"

问题

I'm building a React App,
I'm using Babel and Webpack as module bundler.

While writing the index.js the Application started sending me an error about the dom that is not defined,
but actually I've imported the "react-dom" library as the documentation has proposed to do.
Below I share the code, please help me.

package.json

"dependencies": {
    "react": "^18.2.0",
    "react-dom": "^18.2.0",
    "react-router-dom": "^6.14.1"
  },
  "devDependencies": {
    "@babel/cli": "^7.22.6",
    "@babel/core": "^7.22.6",
    "@babel/preset-env": "^7.22.6",
    "@babel/preset-react": "^7.22.5",
    "autoprefixer": "10.4.5",
    "babel-loader": "^9.1.2",
    "css-loader": "^6.8.1",
    "html-webpack-plugin": "^5.5.3",
    "style-loader": "^3.3.3",
    "webpack-cli": "^5.1.4",
    "webpack-dev-server": "^4.15.1"
  }

index.js

import React from 'react';
import ReactDOM from 'react-dom/client';

const myFirstElement = <h1>Hello React!</h1>

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(myFirstElement);

I've tried to install the library two times and also installed the "react-router-dom" library.
I made a simpler index.js that doesn't render the from App.js, but it still does not work.

I'm expecting to see the project running without problems.

英文:

I'm building a React App,
I'm using Babel and Webpack as module bundler.

While writing the index.js the Application started sending me an error about the dom that is not defined,
but actually I've imported the "react-dom" library as the documentation has proposed to do.
Below I share the code, please help me.

package.json

&quot;dependencies&quot;: {
    &quot;react&quot;: &quot;^18.2.0&quot;,
    &quot;react-dom&quot;: &quot;^18.2.0&quot;,
    &quot;react-router-dom&quot;: &quot;^6.14.1&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;@babel/cli&quot;: &quot;^7.22.6&quot;,
    &quot;@babel/core&quot;: &quot;^7.22.6&quot;,
    &quot;@babel/preset-env&quot;: &quot;^7.22.6&quot;,
    &quot;@babel/preset-react&quot;: &quot;^7.22.5&quot;,
    &quot;autoprefixer&quot;: &quot;10.4.5&quot;,
    &quot;babel-loader&quot;: &quot;^9.1.2&quot;,
    &quot;css-loader&quot;: &quot;^6.8.1&quot;,
    &quot;html-webpack-plugin&quot;: &quot;^5.5.3&quot;,
    &quot;style-loader&quot;: &quot;^3.3.3&quot;,
    &quot;webpack-cli&quot;: &quot;^5.1.4&quot;,
    &quot;webpack-dev-server&quot;: &quot;^4.15.1&quot;
  }

index.js

import React from &#39;react&#39;;
import ReactDOM from &#39;react-dom/client&#39;;

const myFirstElement = &lt;h1&gt;Hello React!&lt;/h1&gt;

const root = ReactDOM.createRoot(document.getElementById(&#39;root&#39;));
root.render(myFirstElement);

I've tryed to install two times the library and installed the other one so called "react-router-dom".
I made a simpler index.js that doesn't render the &lt;App/&gt; from App.js, but It still does not work.

I'm expecting to see the projects running without problems.

答案1

得分: 0

你的ReactDom导入语句似乎存在问题。请不要从&quot;react-dom/client&quot;导入,而应该从&quot;react-dom&quot;导入。如果你进行了更改,请告诉我更改是否有效。

英文:

Your ReactDom import statement seems to be the issue. Instead of importing ReactDom from &quot;react-dom/client&quot;; import it from &quot;react-dom&quot;;. Please let me know if the change worked if/when you make the change.

答案2

得分: 0

为了解决这个问题,我在线寻求帮助,经过多个小时的研究,发现问题出在错误创建的bable文件中。

英文:

In order to solve this problem I've asked for help online, after many hours of researching the issue was found inside the bable file that was bad created.

huangapple
  • 本文由 发表于 2023年7月6日 16:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76626865.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定