警告:React.createElement:type不应为null

时间:2016-12-21 17:10:48

标签: reactjs react-native

我正在使用reactJS创建我的第一个应用程序,但我一直收到此错误:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM element) or a ReactClass (for composite components). Check the render method of 'App'

我已经阅读了十几个问题,但没有一个能帮助我解决这个问题。 这是我的文件:

我的 app.js 文件:

import React, { Component } from 'react';
import { View, Text } from 'react-native';
import { Header } from './components/common';
import { ChooseLevel } from './components/ChooseLevel';

class App extends Component {

  renderContent() {
    <ChooseLevel/>
  }

  render() {
    return (
      <View>
        <Header headerText="my app" />
        {this.renderContent()}
      </View>
    );
  }
}

export default App;

ChooseLevel.js

import React, { Component } from 'react';
import { Text, View } from 'react-native';

class ChooseLevel extends Component {

  render() {
    var rows = [];

    for (var i=1; i <= 10; i++) {
        rows.push(
          <button style={styles.squareStyle}> 
            Level   
          </button>
        );
    }


    return (
      <div>
        {rows}
      </div>
    );
  }
}

const styles = {
  squareStyle: {
    flex:0.3,
    height:60
  }
};

export default ChooseLevel;

1 个答案:

答案 0 :(得分:5)

您正在进行默认导出:#include <vector> #include <iostream> #include <string> #include <boost/range.hpp> // simulate the library class struct X { auto err_begin() const { return errors.begin(); } auto err_end() const { return errors.end(); } std::vector<std::string> errors; }; // provide a generator to build an iterator range auto errors(const X& x) { return boost::make_iterator_range(x.err_begin(), x.err_end()); } // do things with the iterator_range int main() { X x; for (const auto& err : errors(x)) { std::cout << err << std::endl; } std::cout << empty(errors(x)) << std::endl; } 但导入其他内容:export default ChooseLevel;

import { ChooseLevel } from './components/ChooseLevel';大致转换为import { ChooseLevel } from './components/ChooseLevel';

要导入默认导出值,您需要const ChooseLevel = require('./components/ChooseLevel').ChooseLevel;

看看http://es6-features.org/#Constants它详细解释了每个ES6功能。