14

我正在将 React 与 Webpack 和 Babel 一起使用。我收到运行时错误:

未捕获的 ReferenceError:未定义 ReactDom

我的反应版本是:

"devDependencies": {
   "phantomjs-polyfill": "0.0.2",
   "react-addons-test-utils": "^0.14.8"
 },    
 "dependencies": {
   "react": "^0.14.7",
   "react-dom": "^0.14.7"
 },

我的代码是:

import React from 'react';
import ReactDOM from 'react-dom';
import Main from './components/main';

ReactDom.render(<Main />, document.getElementById('root'));

我究竟做错了什么?

4

3 回答 3

41

你有ReactDOM但你没有ReactDom(区分大小写)

ReactDOM.render(<Main />, document.getElementById('root'));
于 2016-04-12T14:57:03.033 回答
4

ReactDOM != ReactDom。你可以做两件事来解决这个问题。

import React from 'react';
import ReactDOM from 'react-dom'; // you used 'react-dom' as 'ReactDOM'
import Main from './components/main';

ReactDom.render(<Main />, document.getElementById('root')); // you referred to it as 'ReactDom' which is wrong.

因为ReactDOM!= ReactDom。因此,您需要修复其中一个地方,以便两个地方具有相同的名称和相同的大小写。

推荐的修复方法是,

import React from 'react';
import ReactDOM from 'react-dom'; // this is recommended 
import Main from './components/main';

ReactDOM.render(<Main />, document.getElementById('root'));

或者你可以这样做,

import React from 'react';
import ReactDom from 'react-dom';
import Main from './components/main';

ReactDom.render(<Main />, document.getElementById('root'));
于 2018-09-24T07:24:20.147 回答
1

您编写的代码中有一个小错误是 ReactDom 而不是 ReactDOM。正确的代码应该在下面给出:

import React from 'react';
import ReactDOM from 'react-dom';
import Main from './components/main';

ReactDOM.render(<Main />, document.getElementById('root'));
于 2021-11-17T06:31:38.170 回答