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

时间:2015-03-25 19:45:04

标签: reactjs

期望下面的代码将无序列表及其子<"li" />个节点附加到document.body,<"ul" />但没有任何子节点并且出现错误消息控制台:

React.createElement: type should not be null or undefined. It should be a string (for DOM elements) or a ReactClass (for composite components).

(React.js ver.0.13)

var ListView = React.createClass({
    render: function() {
        var items = this.props.data.map(function(item) {
            return ItemDelegate({key:item.id, data:item.id});
        }.bind(this));
        return (React.createElement('ul', null, items));
    }
});

var ItemDelegate = React.createFactory(ListViewChildren);

var ListViewChildren = React.createClass({
    render: function(){
        return (React.createElement('li', {key: this.props.key, data: this.props.data}));
    }
});

var Wrapper = React.createClass({
    render: function() {
        return (React.createElement(ListView, {data: [/*some data*/]}));
    }
});

React.render(React.createElement(Wrapper), document.body);

类型是什么?必须声明所声明的类型在哪里?我想在某个地方完全误解了这个概念,但在哪里呢?

1 个答案:

答案 0 :(得分:15)

警告来自

var ItemDelegate = React.createFactory(ListViewChildren);

ListViewChildren定义如下,因此React无法创建工厂。

只需移动上方var ListViewChildren = ...,警告就会消失。

ListView课程中也存在问题:您的items应该是儿童,而不是道具:

// items as props (2nd arg): won't work, html elements don't have props :)
return (React.createElement('ul', items));
// items as children (3rd arg) should work better
return (React.createElement('ul', null, items));

完整代码:http://jsfiddle.net/Lmb7t9pv/2/