我需要将我的无状态功能组件重构为一个类。但是,当我这样做时,我总是得到一个错误,看起来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,以防我发布的代码与此无关。
谢谢!
发布于 2016-05-21 15:39:51
哦..。
import { React, Component } from 'react';
需要的是
import React, { Component } from 'react';
:)
发布于 2016-12-14 03:43:42
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本身获取组件,因此您可能只需:
import React from 'react'如果React.Component不那么令人迷惑,请改用它。
发布于 2017-06-21 22:16:01
对于我在TypeScript中的解决方案是:
import * as React from 'react';https://stackoverflow.com/questions/37360202
复制相似问题