继react.js教程中,我已经得到了一个错误:Uncaught TypeError: Cannot read property 'map' of undefined
。
我严格遵循教程,但坚持从服务器部分获取。当我使用 url 数据而不是硬编码的 JSON 数据提供 commentBox 时会出现错误。
/** @jsx React.DOM */
var converter = new Showdown.converter();
var data = [
{ Author: "Daniel Lo Nigro", Text: "Hello ReactJS.NET World!" },
{ Author: "Pete Hunt", Text: "This is one comment" },
{ Author: "Jordan Walke", Text: "This is *another* comment" }
];
var Comment = React.createClass({
render: function() {
var rawMarkup = converter.makeHtml(this.props.children.toString());
return (
<div className="comment">
<h2 className="commentAuthor">
{this.props.author}
</h2>
<span dangerouslySetInnerHTML={{__html: rawMarkup}} />
</div>
);
}
});
var CommentList = React.createClass({
render: function() {
var commentNodes = this.props.data.map(function (comment) {
return <Comment author={comment.Author}>{comment.Text}</Comment>;
});
return (
<div className="commentList">
{commentNodes}
</div>
);
}
});
var CommentForm = React.createClass({
render: function() {
return (
<div className="commentForm">
Hello, world! I am a CommentForm.
</div>
);
}
});
var CommentBox = React.createClass({
render: function() {
return (
<div className="commentBox">
<h1>Comments</h1>
<CommentList data={this.props.data} />
<CommentForm />
</div>
);
}
});
React.renderComponent(
//<CommentBox data={data}/>, //this works fine
<CommentBox url="/comments" />, //Changing data feet to url produces an error
document.getElementById('content')
);
请求http://localhost:52581/comments
正在工作并返回 JSON 数据:
[{"Author":"Daniel Lo Nigro","Text":"Hello ReactJS.NET World!"},{"Author":"Pete Hunt","Text":"This is one comment"},{"Author":"Jordan Walke","Text":"This is *another* comment"}]
任何建议都会对我有很大帮助。谢谢。