I'm trying to learn react.js and found a tutorial online. I was following a react/rails tutorial from http://rny.io/rails/react/2014/07/31/reactjs-and-rails.html
The all the code was working fine until I got to the last step implementing the Comment Form. After following all the instructions, I get an error in my chrome console pointing to this line of code var commentNodes = this.props.comments.map(function (comment, index) { saying its "Uncaught TypeError: undefined is not a function". The form shows up and accepts input but nothing is displayed after I submit. Also, the tutorial is a bit dated, it is still using React.renderComponent, I changed it to React.render after reading the docs. Is there some more deprecated code I missed? or Can anyone help me or tell me what I did wrong?
Thanks in Advance
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);
The likely reason is that the comments coming back is not an array. Do a console.log(comments) or debugger; before this line: this.setState({comments: comments}); to check your ajax response and see if comments is an array of comments. If it's anything other than array, then that's your problem, you can just put some mock data in there for now until you can get that working.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With