反应.js:未捕获类型错误:未定义不是一个函数

React.js : Uncaught TypeError: undefined is not a function

本文关键字:函数 一个 未定义 js 错误 类型 反应      更新时间:2023-09-26

我正在尝试学习反应.js并在网上找到了一个教程。我正在遵循 http://rny.io/rails/react/2014/07/31/reactjs-and-rails.html 的反应/轨道教程

所有代码都运行良好,直到我进入实现注释表单的最后一步。按照所有说明操作后,我在chrome控制台中收到一个错误,指向这行代码var commentNodes = this.props.comments.map(function (comment, index) {说它的"未捕获的类型错误:未定义不是一个函数"。表单显示并接受输入,但在我提交后不显示任何内容。另外,教程有点过时了,它仍在使用React.renderComponent,我在阅读文档后将其更改为React.render。我是否错过了一些更多已弃用的代码?或者谁能帮助我或告诉我我做错了什么?提前致谢

var Comment = React.createClass({
      render: function () {
        return (
          <div className="comment">
            <h2 className="commentAuthor">
              {this.props.author}
            </h2>
              {this.props.comment}
          </div>
      );
  }
});
var CommentList = React.createClass({
  render: function () {
    var commentNodes = this.props.comments.map(function (comment, index) {
      return (
        <Comment author={comment.author} comment={comment.comment} key={index} />
        );
    });
    return (
      <div className="commentList">
        {commentNodes}
      </div>
      );
  }
});
var CommentBox = React.createClass({
  getInitialState: function () {
    return {comments: []};
  },
  componentDidMount: function () {
    this.loadCommentsFromServer();
  },
  loadCommentsFromServer: function () {
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      success: function (comments) {
        this.setState({comments: comments});
      }.bind(this),
      error: function (xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  handleCommentSubmit: function(comment) {
    var comments = this.state.comments;
    var newComments = comments.concat([comment]);
    this.setState({comments: newComments});
    $.ajax({
      url: this.props.url,
      dataType: 'json',
      type: 'POST',
      data: {"comment": comment},
      success: function(data) {
        this.loadCommentsFromServer();
      }.bind(this),
      error: function(xhr, status, err) {
        console.error(this.props.url, status, err.toString());
      }.bind(this)
    });
  },
  render: function () {
    return (
      <div className="commentBox">
        <h1>Comments</h1>
        <CommentList comments={this.state.comments} />
        <CommentForm onCommentSubmit={this.handleCommentSubmit}/>
      </div>
      );
  }
});
var CommentForm = React.createClass({
  handleSubmit: function() {
    var author = this.refs.author.getDOMNode().value.trim();
    var comment = this.refs.comment.getDOMNode().value.trim();
    this.props.onCommentSubmit({author: author, comment: comment});
    this.refs.author.getDOMNode().value = '';
    this.refs.comment.getDOMNode().value = '';
    return false;
  },
  render: function() {
    return (
      <form className="commentForm" onSubmit={this.handleSubmit}>
        <input type="text" placeholder="Your name" ref="author" />
        <input type="text" placeholder="Say something..." ref="comment" />
        <input type="submit" value="Post" />
      </form>
      );
  }
});
var ready = function () {
  React.render(
    <CommentBox url="/comments.json" />,
    document.getElementById('comments')
  );
};
$(document).ready(ready);

可能的原因是返回的注释不是一个数组。在此行之前执行console.log(comments)debugger;this.setState({comments: comments});检查您的 ajax 响应并查看注释是否是注释数组。如果它不是数组,那么这就是你的问题,你可以暂时把一些模拟数据放在那里,直到你可以让它工作。