期望下面的代码附加无序列表及其子
节点到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);
它是什么类型,在哪里声明类型必须被声明?我想这个概念有一个完全的误会,但在哪里?
警告来自
原文链接:https://www.f2er.com/react/301630.htmlvar ItemDelegate = React.createFactory(ListViewChildren);
ListViewChildren在下面定义,所以React无法创建工厂。
只要移动上面的var ListViewChildren = …并且警告就会消失。
您在ListView类中还有一个问题:您的项目应该是子代而不是道具:
// 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',items));