未捕获(承诺)TypeError:无法读取未定义(...)的属性“createElement”

新手上路,请多包涵

我需要将我的无状态功能组件重构为一个类。但是,当我这样做时,我不断收到一个错误,看起来 React 本身是未定义的。

 import React from 'react';
import { Cell } from 'fixed-data-table';

const DataCell = ({rowIndex, columnKey, data, onMessageClicked, ...props}) => {
  return (
    <Cell {...props} onClick={onMessageClicked(data[rowIndex].Id)}>
      {data[rowIndex][columnKey]}
    </Cell>
  );
};

export default DataCell;

import { React, Component } from 'react';
import { Cell } from 'fixed-data-table';

class DataCell extends Component {

  onCellClicked() {
    this.props.onMessageClicked(this.props.data[this.props.rowIndex].Id);
  }

  render() {
    const {rowIndex, columnKey, data, ...props} = this.props;
    return (
      <Cell {...props} onClick={onCellClicked}>
        {data[rowIndex][columnKey]}
      </Cell>
    );
  }
}

export default DataCell;

bundle.js:43248 Uncaught (in promise) TypeError: Cannot read property 'createElement' of undefined(…)

当我走到那条线时,我看到

return _react.React.createElement(

我不明白。我该如何调试/修复这个问题?

我的这个应用程序的完整代码在 这里,以防我发布的代码以某种方式不相关。

谢谢!

原文由 magician11 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 334
2 个回答

哦…

import { React, Component } from 'react';

需要是

import React, { Component } from 'react';

:)

原文由 magician11 发布,翻译遵循 CC BY-SA 3.0 许可协议

OP 已经自己回答了这个问题,但没有理由,所以这就是原因! {直接引用自 https://github.com/facebook/react/issues/2607#issuecomment-225911048 》 }

 import { React, Component } from 'react';

不正确,应该是

import React, { Component } from 'react';

没有名为 React 的命名导出。这很令人困惑,因为 React 是一个 CommonJS 模块,并且由于您使用的是 ES6 导入,Babel 会尝试映射语义,但它们并不完全匹配。 { Component } 实际上是从 React 本身获取 Component 所以你可能只是:

 import React from 'react'

并使用 React.Component 如果它不那么混乱的话。

原文由 Kunal 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题