首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >未捕获(in promise) TypeError:无法读取未定义(…)的属性“”createElement“”

未捕获(in promise) TypeError:无法读取未定义(…)的属性“”createElement“”
EN

Stack Overflow用户
提问于 2016-05-21 15:35:44
回答 3查看 14.9K关注 0票数 24

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

代码语言:javascript
复制
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;

代码语言:javascript
复制
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,以防我发布的代码与此无关。

谢谢!

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-05-21 15:39:51

哦..。

import { React, Component } from 'react';

需要的是

import React, { Component } from 'react';

:)

票数 86
EN

Stack Overflow用户

发布于 2016-12-14 03:43:42

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

代码语言:javascript
复制
import { React, Component } from 'react';

是不正确的,应该是

代码语言:javascript
复制
import React, { Component } from 'react';

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

代码语言:javascript
复制
import React from 'react'

如果React.Component不那么令人迷惑,请改用它。

票数 23
EN

Stack Overflow用户

发布于 2017-06-21 22:16:01

对于我在TypeScript中的解决方案是:

代码语言:javascript
复制
import * as React from 'react';
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37360202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档