javascript - React.createElement : type should not be null, undefined..创建/渲染组件时

标签 javascript reactjs

所以完整的错误如下......

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/

相关文章:

javascript - 如何消除错误: "Implied eval is evil"

javascript - 如何使用多个表数据仅渲染一次页面而它们之间没有关系?

reactjs - react 原生裁剪图像

css - 如何使文本与图标在同一行?

reactjs - 'string | undefined' 类型的参数不能分配给 'string' 类型的参数。类型 'undefined' 不可分配给类型 'string'

javascript - Nodejs 未部署在 heroku 上 - node-pre-gyp install --fallback-to-build

javascript - 错误: $injector:modulerr AngularJS

javascript - 如何在 JavaScript 中进行 "raw"字符串搜索和替换,无正则表达式

reactjs - ES6 React istanbul 未收集覆盖率信息,退出时不写入覆盖率信息

javascript - jQuery UI 可调整大小的反向