I'm trying to learn react.js and found a tutorial online. I was following a react/rails tutorial from .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 mentNodes = this.propsments.map(function (ment, 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="ment">
<h2 className="mentAuthor">
{this.props.author}
</h2>
{this.propsment}
</div>
);
}
});
var CommentList = React.createClass({
render: function () {
var mentNodes = this.propsments.map(function (ment, index) {
return (
<Comment author={ment.author} ment={mentment} key={index} />
);
});
return (
<div className="mentList">
{mentNodes}
</div>
);
}
});
var CommentBox = React.createClass({
getInitialState: function () {
return {ments: []};
},
ponentDidMount: function () {
this.loadCommentsFromServer();
},
loadCommentsFromServer: function () {
$.ajax({
url: this.props.url,
dataType: 'json',
success: function (ments) {
this.setState({ments: ments});
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
handleCommentSubmit: function(ment) {
var ments = this.statements;
var newComments = ments.concat([ment]);
this.setState({ments: newComments});
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: {"ment": ment},
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="mentBox">
<h1>Comments</h1>
<CommentList ments={this.statements} />
<CommentForm onCommentSubmit={this.handleCommentSubmit}/>
</div>
);
}
});
var CommentForm = React.createClass({
handleSubmit: function() {
var author = this.refs.author.getDOMNode().value.trim();
var ment = this.refsment.getDOMNode().value.trim();
this.props.onCommentSubmit({author: author, ment: ment});
this.refs.author.getDOMNode().value = '';
this.refsment.getDOMNode().value = '';
return false;
},
render: function() {
return (
<form className="mentForm" onSubmit={this.handleSubmit}>
<input type="text" placeholder="Your name" ref="author" />
<input type="text" placeholder="Say something..." ref="ment" />
<input type="submit" value="Post" />
</form>
);
}
});
var ready = function () {
React.render(
<CommentBox url="/ments.json" />,
document.getElementById('ments')
);
};
$(document).ready(ready);
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 mentNodes = this.props.ments.map(function (ment, 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="ment">
<h2 className="mentAuthor">
{this.props.author}
</h2>
{this.props.ment}
</div>
);
}
});
var CommentList = React.createClass({
render: function () {
var mentNodes = this.props.ments.map(function (ment, index) {
return (
<Comment author={ment.author} ment={ment.ment} key={index} />
);
});
return (
<div className="mentList">
{mentNodes}
</div>
);
}
});
var CommentBox = React.createClass({
getInitialState: function () {
return {ments: []};
},
ponentDidMount: function () {
this.loadCommentsFromServer();
},
loadCommentsFromServer: function () {
$.ajax({
url: this.props.url,
dataType: 'json',
success: function (ments) {
this.setState({ments: ments});
}.bind(this),
error: function (xhr, status, err) {
console.error(this.props.url, status, err.toString());
}.bind(this)
});
},
handleCommentSubmit: function(ment) {
var ments = this.state.ments;
var newComments = ments.concat([ment]);
this.setState({ments: newComments});
$.ajax({
url: this.props.url,
dataType: 'json',
type: 'POST',
data: {"ment": ment},
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="mentBox">
<h1>Comments</h1>
<CommentList ments={this.state.ments} />
<CommentForm onCommentSubmit={this.handleCommentSubmit}/>
</div>
);
}
});
var CommentForm = React.createClass({
handleSubmit: function() {
var author = this.refs.author.getDOMNode().value.trim();
var ment = this.refs.ment.getDOMNode().value.trim();
this.props.onCommentSubmit({author: author, ment: ment});
this.refs.author.getDOMNode().value = '';
this.refs.ment.getDOMNode().value = '';
return false;
},
render: function() {
return (
<form className="mentForm" onSubmit={this.handleSubmit}>
<input type="text" placeholder="Your name" ref="author" />
<input type="text" placeholder="Say something..." ref="ment" />
<input type="submit" value="Post" />
</form>
);
}
});
var ready = function () {
React.render(
<CommentBox url="/ments.json" />,
document.getElementById('ments')
);
};
$(document).ready(ready);
Share
Improve this question
asked Mar 16, 2015 at 6:17
Jason TJason T
571 gold badge1 silver badge7 bronze badges
2
- do you have a server setup that is returning a JSON data object that contains a list called ments? – Chris Hawkes Commented Mar 16, 2015 at 15:28
-
@ChrisHawkes thanks for your reply. I checked my controller etc and it is ok. It turns out the app was just missing one word.
response
, see answer below. Thanks! – Jason T Commented Mar 17, 2015 at 1:13
1 Answer
Reset to default 5The likely reason is that the ments ing back is not an array. Do a console.log(ments)
or debugger;
before this line: this.setState({ments: ments});
to check your ajax response and see if ments is an array of ments. 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.