【问题标题】:Uncaught (in promise) TypeError: Cannot read property 'createElement' of undefined(…)未捕获(承诺中)类型错误:无法读取未定义的属性“createElement”(…)
【发布时间】:2016-05-21 07:35:44
【问题描述】:

我需要将我的无状态功能组件重构为一个类。但是,当我这样做时,我不断收到一个错误,看起来 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(

我不明白。如何调试/修复此问题?

我的这个应用程序的完整代码是here,以防我发布的代码不相关。

谢谢!

【问题讨论】:

    标签: javascript reactjs


    【解决方案1】:

    哦...

    import { React, Component } from 'react';

    需要

    import React, { Component } from 'react';

    :)

    【讨论】:

    • 今天仍然有用,但有点让人头疼。谢谢!
    【解决方案2】:

    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

    【讨论】:

      【解决方案3】:

      对我来说,TypeScript 的解决方案是:

      import * as React from 'react';
      

      【讨论】:

      • 我使用的是 Typescript 2.1.5 版本,这对我有帮助。我认为 tsconfig.json 有问题。也许types: ['node'] 也会有所帮助。
      猜你喜欢
      • 1970-01-01
      • 2018-10-29
      • 2020-12-15
      • 2017-08-12
      • 2021-05-13
      • 1970-01-01
      • 2023-03-31
      • 2020-11-09
      • 2018-08-26
      相关资源
      最近更新 更多