admin管理员组文章数量:1333632
I have written code from the ReactJs site here while working through their ment box tutorial. Yet for some reason it is giving me an error about map:
Uncaught TypeError: this.props.data.map is not a function
Specifically it is plaining about line 7: var mentNodes = this.props.data.map(function (ment){ .. etc.. }
This error is particularly confusing since data is a list. Here is the full code displaying that:
var CommentList = React.createClass({
render: function(){
console.log(data)
var mentNodes = this.props.data.map(function (ment){
return (<Comment author={ment.author}>
{ment.text}
</Comment>);
});
return (<div className="mentList">
{mentNodes}
</div>);
}
});
var Comment = React.createClass({
render: function(){
return (<div className="ment">
<h2 className="mentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div> );
}
});
var CommentForm = React.createClass({
render: function(){
return (<div className="mentForm">I am a ment form!</div>);
}
});
var CommentBox = React.createClass({
render: function() {
return (<div className="mentBox">
<CommentList data="{this.props.data}" />
<CommentForm />
</div>);
}
});
var data = [
{author: "Pete Hunt", text: "This is one ment"},
{author: "Jordan Walke", text: "This is *another* ment"}
];
React.render(<CommentBox data={data} />, document.getElementById('content'));
I have written code from the ReactJs site here while working through their ment box tutorial. Yet for some reason it is giving me an error about map:
Uncaught TypeError: this.props.data.map is not a function
Specifically it is plaining about line 7: var mentNodes = this.props.data.map(function (ment){ .. etc.. }
This error is particularly confusing since data is a list. Here is the full code displaying that:
var CommentList = React.createClass({
render: function(){
console.log(data)
var mentNodes = this.props.data.map(function (ment){
return (<Comment author={ment.author}>
{ment.text}
</Comment>);
});
return (<div className="mentList">
{mentNodes}
</div>);
}
});
var Comment = React.createClass({
render: function(){
return (<div className="ment">
<h2 className="mentAuthor">
{this.props.author}
</h2>
{this.props.children}
</div> );
}
});
var CommentForm = React.createClass({
render: function(){
return (<div className="mentForm">I am a ment form!</div>);
}
});
var CommentBox = React.createClass({
render: function() {
return (<div className="mentBox">
<CommentList data="{this.props.data}" />
<CommentForm />
</div>);
}
});
var data = [
{author: "Pete Hunt", text: "This is one ment"},
{author: "Jordan Walke", text: "This is *another* ment"}
];
React.render(<CommentBox data={data} />, document.getElementById('content'));
Share
Improve this question
asked Jun 12, 2015 at 21:27
ApathyBearApathyBear
9,61515 gold badges59 silver badges90 bronze badges
1 Answer
Reset to default 9This shouldn't be wrapped in a string data="{this.props.data}"
. Just remove the quotes and it should work.
本文标签: javascriptTypeError thispropsdatamap is not a functionbut data is a listStack Overflow
版权声明:本文标题:javascript - TypeError: this.props.data.map is not a function, but data is a list - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742343522a2457061.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论