当前位置: 首页 > 面试题库 >

ReactJS失败,并显示错误“无法读取未定义的属性'map'”

黄德明
2023-03-14
问题内容

在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"}]

任何建议对我都会有很大帮助。谢谢。


问题答案:

在CommentBox中,此行是问题所在: <CommentList data={this.props.data} />

您不再需要,props.data因为您传入的是URL而不是JS对象。

您的第二个作品之所以有效,是因为您使用state并将默认值设置为一个空数组,但仍可以在其上运行map。



 类似资料:
  • 我基本上是React的初学者。我有一个仪表板页面,其中显示了一个React表。我有一个自定义按钮,它将打开一个弹出页面,这个弹出页面有一些复选框允许我显示/隐藏那些反应列。最初,此弹出页面中的所有复选框都设置为true。当我取消选中某列时,该列将被禁用。 这是我的父组件-父页面是带有ReactTable的页面,现在有10列,而不是像图中所示的8列。 这是我的子组件--在我的子页面(显示复选框的页面

  • 当我在react中工作时。当我想要侧边页时,我会显示“TypeError:不能读取未定义的属性(读取'pathname')”!

  • 我不熟悉量角器测试。似乎缺少getWebelement。我的保护者的版本是3.0。0.其他属性正常(如单击、评估…) 或者 (c:\Users\xxx\AppData\Roaming\npm\node\u modules\dragor\lib\element.js:754:36)位于c:\Users\xxx\AppData\Roaming\npm\node\u modules\dragor\nod

  • 问题内容: 我正在通过教程学习reactjs并遇到此错误。那就是“无法读取未定义的属性’keys’”。我的代码非常少,因此我认为它与语言的结构有关。有谁知道这个问题和可能的解决方案? 问题答案: 编辑:奇怪的是,在我们上面的评论之后,我检查了一下它是否确实是babel核心版本,我在小提琴中使用了这个版本: 我在上面第二个切换到您的版本时得到以下信息: 使用not 并在括号中包裹多行html,如下所

  • 运行此代码时,在的第一行出现错误 TypeError:无法读取未定义的属性“array” 代码: 我试着搜索,但没有得到正确的解。

  • 问题内容: 运行此代码时,我在第一行出现错误 TypeError:无法读取未定义的属性“数组” 码: 我尝试搜索,但没有得到正确的解决方案。 问题答案: 道具类型现在是一个单独维护的库,名称为 这里是react- docs的解释:https : //reactjs.org/docs/typechecking-with- proptypes.html 您必须将它们导入为 NPM套餐

  • 有人可以解释为什么我得到一个错误,当我使用此语法 但是当我使用箭头指针语法时没有错误,如下所示 注意:在这两种情况下,我都使用过 在 内。