【问题标题】:Warning: React.createElement: type should not be null, undefined, boolean, or number警告:React.createElement:类型不应为 null、未定义、布尔值或数字
【发布时间】:2016-01-28 10:06:24
【问题描述】:

警告:React.createElement:类型不应为 null、未定义、布尔值或数字。它应该是一个字符串(对于 DOM 元素)或一个 ReactClass(对于复合组件)。

我目前正在将一个反应页面放在一起,当我尝试使用 es2015 样式代码时遇到了上述错误。当我使用以下代码时,我得到了错误

var React = require('react');
export default class StoreApp extends React.Component {
  getInitialState() {
    return null;
  }

  render() {
    return (
      <div>
        <p>This should display on the screen</p>
      </div>
    );
  }
}

这很好用……

var React = require('react');

var StoreApp = React.createClass({
  render: function() {
    return (
      <div>
        <p>This should display on the screen</p>
      </div>
    );
  }
});

module.exports = StoreApp;

我正在使用带有以下预设的 babel ['react','es2015'] 有什么想法吗?

【问题讨论】:

    标签: javascript reactjs webpack babeljs


    【解决方案1】:

    在 es6 中你不能使用getInitialState()。使用constructor()

    例如

    constructor(props) {
      super(props);
      //Set your state here using this.state = {};
      //or leave blank if you don't want anything set in state
    }
    

    如果您没有将任何内容置于状态中,您甚至可以将构造函数留在 es6 类中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 2017-01-20
      • 1970-01-01
      • 1970-01-01
      • 2015-10-25
      • 2016-09-10
      相关资源
      最近更新 更多