【问题标题】:React and Meteor "Uncaught TypeError: Cannot read property 'createElement' of undefined"反应和流星“未捕获的类型错误:无法读取未定义的属性'createElement'”
【发布时间】:2018-12-26 20:33:27
【问题描述】:

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

  • 从组件的导入语句中删除大括号
  • 导入的 React 没有大括号
  • 我用大括号试过了

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 更改为函数而不是类,它将起作用,但 react 在文档中说它没有区别。我还尝试将类的构造函数部分全部注释掉/删除,但没有任何效果。我有点不知所措,为什么这不起作用。为什么它不能作为一个类工作?

【问题讨论】:

    标签: javascript reactjs meteor


    【解决方案1】:

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

    import React, { Component } from 'react';
    

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-07
      • 1970-01-01
      • 2018-11-09
      • 2021-06-14
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      相关资源
      最近更新 更多