React and Meteor "Uncaught TypeError: Cannot read property 'createElement' of undefined"

React and Meteor "Uncaught TypeError: Cannot read property 'createElement' of undefined"

我已经看过之前提出的问题,并且我已经完成了所有建议的事情:

index.hmtl

<head>
    <title>My ChatApp</title>
</head>

<body>
    <div id="app"></div>
</body>

index.js

import { Meteor } from "meteor/meteor";
import React from "react";
import { render } from "react-dom";

import ChatComponent from "../../ui/App";

Meteor.startup(() => {
  render(<ChatComponent />, document.getElementById("app"));
});

App.js

import { React, Component } from "react";

class ChatComponent extends Component {
  constructor(props) {
    super(props);
    this.state = {};
  }
  render() {
    return <h1>Try Chatting</h1>;
  }
}

export default ChatComponent;

我仍然从 App.js 文件的第 9 行(即 "return" 行)收到错误 "Uncaught TypeError: Cannot read property 'createElement' of undefined"。如果我将 ChatComponent 更改为函数而不是 class,它将起作用,但 React 在文档中表示它不区分。我也试过 commenting-out/removing-all-together class 的构造函数部分但没有效果。我有点不知所措,为什么这不起作用。为什么它不能像 class 一样工作?

App.js 中,您想将此导入行用于 React:

import React, { Component } from 'react';

原因是 jsx 语法被编译成 React.createElement 调用。

因此错误 Cannot read property 'createElement' of undefined 告诉您 React 未定义。

在React包中,React是默认导出的,所以不能在花括号中导入