javascript - TypeError : this. props.data.map 不是一个函数,但数据是一个列表

标签 javascript reactjs

我已经从 ReactJs 站点编写了代码 here在完成他们的评论框教程时。但是由于某种原因,它给我一个关于 map 的错误:

Uncaught TypeError: this.props.data.map is not a function

特别是它提示第 7 行:var commentNodes = this.props.data.map(function (comment){ .. etc.. }

这个错误特别令人困惑,因为数据一个列表。以下是显示的完整代码:

            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'));

最佳答案

这不应包含在字符串 data="{this.props.data}" 中。只需删除引号即可。

关于javascript - TypeError : this. props.data.map 不是一个函数,但数据是一个列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30812432/

相关文章:

javascript - Browserify v2 的全局要求

javascript - webpack 在react.js 中无法正常工作

reactjs - 如何使用提供的操作通过模拟 Axios 编写一个 Jest 测试?

javascript - 如何在不同事件中将多个数据字符串添加到相同类型的数组到本地存储

javascript - 具有图像背景和完全响应的 JQUERY UI slider

javascript - Chrome onMessage 监听器没有被调用

javascript - blur() 无法以编程方式工作 |但是当从用户发起时

reactjs - 如何将 DeckGL 与 React TypeScript 集成?

javascript - 有条件加载 Reactjs 组件时减少代码冗余

javascript - 在 react-select 中的 input 元素前添加一个图标