[英]Gettting React.createElement: type is invalid — expected a string … but got: undefined
I am trying to copy this example using react data grid into my Typescript project. 我试图使用反应数据网格将此示例复制到我的Typescript项目中。 I created the following class to match the one provided....
我创建了以下类来匹配提供的类....
import * as React from "react";
import ReactDataGrid from "react-data-grid";
interface PxGridProps {}
interface PxGridState {}
export default class PxGrid extends React.Component<PxGridProps, PxGridState>{
private _columns;
private _rows;
constructor(props) {
super(props);
this.props = props;
this.createRows();
this._columns = [
{ key: 'id', name: 'ID' },
{ key: 'title', name: 'Title' },
{ key: 'count', name: 'Count' } ];
return null;
}
createRows() {
let rows = [];
for (let i = 1; i < 1000; i++) {
rows.push({
id: i,
title: 'Title ' + i,
count: i * 1000
});
}
this._rows = rows;
}
rowGetter(i) {
return this._rows[i];
}
render() {
return (
<ReactDataGrid
minHeight={500}
columns={this._columns}
rowGetter={this.rowGetter.bind(this)}
rowsCount={this._rows.length}
/>
);
}
}
But when I try to run it I get 但是当我尝试运行它时,我得到了
Warning: React.createElement: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: undefined.
警告:React.createElement:type无效 - 期望一个字符串(对于内置组件)或一个类/函数(对于复合组件)但得到:undefined。 You likely forgot to export your component from the file it's defined in. Check the render method of
PxGrid
.您可能忘记从其定义的文件中导出组件。检查
PxGrid
的render方法。
If I replace out the render to something like this.... 如果我将渲染替换为这样的东西....
render() {
return (
<h1> Thing </h1>
);
}
It works fine. 它工作正常。 What am I getting wrong?
我错了什么? The error message is not helping at all.
错误消息根本没有帮助。
This was an issue importing. 这是一个导入问题。 Not sure what was causing it but when I have
import ReactDataGrid from "react-data-grid";
不确定是什么导致它,但是当我
import ReactDataGrid from "react-data-grid";
时import ReactDataGrid from "react-data-grid";
and debug on 并调试
render() {
return (
<ReactDataGrid
minHeight={500}
columns={this._columns}
rowGetter={this.rowGetter.bind(this)}
rowsCount={this._rows.length}
/>
);
}
I see ReactDataGrid is undefined. 我看到ReactDataGrid未定义。 For some reason I needed to change my import to...
出于某种原因,我需要将导入更改为...
import * as ReactDataGrid from "react-data-grid";
And everything started working fine. 一切都开始好起来了。
Don't know much about the typescript, check this working code and do the changes, it should work in your case also: 不太了解打字稿,检查这个工作代码并进行更改,它也适用于你的情况:
class Example extends React.Component{ constructor() { super(); this._columns = [ { key: 'id', name: 'ID' }, { key: 'title', name: 'Title' }, { key: 'count', name: 'Count' } ]; this._rows = this.createRows(); } createRows() { let rows = []; for (let i = 1; i < 1000; i++) { rows.push({ id: i, title: 'Title ' + i, count: i * 1000 }); } return rows; } rowGetter(i) { return this._rows[i]; } render() { return ( <ReactDataGrid columns={this._columns} rowGetter={this.rowGetter.bind(this)} rowsCount={this._rows.length} minHeight={500} />); } } ReactDOM.render( <Example />, document.getElementById('container') );
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script> <script type="text/javascript" src="https://npmcdn.com/react-data-grid/dist/react-data-grid.min.js"></script> <div id='container'/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.