Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning: React.createElement: type should not be null or undefined

Tags:

Expecting the below code to attach an unordered list and its child <"li" /> nodes to the document.body, it's OK with the <"ul" /> but without any children and with an error message in the console:

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);

What type is it and where is it that the stated type has to be declared? I guess there's a total misunderstanding of the concept somewhere, but where?

like image 940
nonkertompf Avatar asked Mar 25 '15 19:03

nonkertompf


1 Answers

The warning comes from

var ItemDelegate = React.createFactory(ListViewChildren);

ListViewChildren is defined below so React cannot create the factory.

Just move the var ListViewChildren = ... above and the warning will go away.

You also have an issue in the ListView class: your items should be children and not props:

// 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));

Full code: http://jsfiddle.net/Lmb7t9pv/2/

like image 126
al8anp Avatar answered Sep 23 '22 17:09

al8anp