Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TypeError: this.props.data.map is not a function, but data is a list

I have written code from the ReactJs site here while working through their comment 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 complaining about line 7: var commentNodes = this.props.data.map(function (comment){ .. 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 commentNodes = this.props.data.map(function (comment){
                        return (<Comment author={comment.author}>
                                {comment.text}
                                </Comment>);
                    });
                    return (<div className="commentList">
                            {commentNodes}            
                            </div>);
                }   
            });

            var Comment = React.createClass({
                render: function(){
                    return (<div className="comment">
                            <h2 className="commentAuthor">
                            {this.props.author}
                            </h2>
                            {this.props.children}
                            </div> );
                }
            });            

            var CommentForm = React.createClass({
                render: function(){
                    return (<div className="commentForm">I am a comment form!</div>);    
                }
            });            
            var CommentBox = React.createClass({
                render: function() {
                    return (<div className="commentBox">
                            <CommentList data="{this.props.data}" />
                            <CommentForm />
                            </div>);
                }
            });

            var data = [
                    {author: "Pete Hunt", text: "This is one comment"},
                    {author: "Jordan Walke", text: "This is *another* comment"}
            ];
            React.render(<CommentBox data={data} />, document.getElementById('content'));
like image 462
ApathyBear Avatar asked Jun 12 '15 21:06

ApathyBear


1 Answers

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

like image 181
Giuseppe Pes Avatar answered Oct 09 '22 06:10

Giuseppe Pes