最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - TypeError: this.props.data.map is not a function, but data is a list - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

This shouldn't be wrapped in a string data="{this.props.data}". Just remove the quotes and it should work.

发布评论

评论列表(0)

  1. 暂无评论