所以完整的错误如下......
Warning: React.createElement: type should not be null, undefined,
boolean, or number. It should be a string (for DOM elements) or a ReactClass (for
composite components). Check the render method of `IndexBody.
我不确定为什么会收到此错误,我以为我正确地创建了我的组件,但也许另一只眼睛可以看到我做错了什么。
index.jsx:
import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
import Test from './app';
class IndexBody extends React.Component {
render() {
return (
<div>
<h1>This will show the Test Component</h1>
<Test />
</div>
);
}
}
ReactDOM.render(<IndexBody />, document.getElementById('react-app'))
以及我从 ./app.jsx 导入的测试组件
import React, { PropTypes } from 'react';
import ReactDOM from 'react-dom';
class Test extends React.Component {
constructor(props) {
super(props);
this.state = {
hello: 'hello world',
};
}
render() {
console.log('WORKS'); // logs fine
return (
<div>
<h1>{this.state.hello}</h1>
</div>
);
}
}
ReactDOM.render(<Test/>, document.getElementById('react-app'));
我能够显示日志,但它不想渲染组件。我是否错误地创建了组件?提前感谢您的帮助!
最佳答案
您不从 app.jsx
导出 Test
。
这意味着它不能作为导入使用,因此未定义
。
根据您当前的代码,您需要添加:
export default Test;
到app.jsx
。
关于javascript - React.createElement : type should not be null, undefined..创建/渲染组件时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38669619/