警告: React.createElement: 型は null または undefined であってはなりません 質問する

警告: React.createElement: 型は null または undefined であってはなりません 質問する

以下のコードでは、順序なしリストとその子<"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 バージョン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

警告の出所は

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/

おすすめ記事