[英]React.js TypeError this.props is undefined
I've started trying to learn react and have ran into a problem I can't seem to figure out. 我已经开始尝试学习反应,遇到了一个我似乎无法解决的问题。 Going through a tutorial making a simple comment editing web app and I'm getting this error when I try to update a comment "TypeError: _this3 is undefined", specifically on these lines: 通过制作一个简单的注释编辑Web应用程序的教程,尝试更新注释“ TypeError:_this3 is undefined”时,出现此错误,特别是在以下几行:
this.props.updateCommentText(this.refs.newText.value, this.props.index);
and this one: 还有这个:
updateCommentText={()=>this.updateComment}
Here is the full javascript code: 这是完整的javascript代码:
class Comment extends React.Component{
constructor(){
super();
this.state = {
editing: false,
};
this.edit = this.edit.bind(this);
this.save = this.save.bind(this);
}
edit(){
this.setState({
editing: true,
});
}
save(){
this.props.updateCommentText(this.refs.newText.value, this.props.index);
this.setState({
editing: false,
});
}
remove(){
console.log('Removing comment');
this.props.deleteFromBoard(this.props.index)
}
renderNormal(){
return (
<div className="commentContainer">
<div className="commentText">{this.props.children}</div>
<button onClick={this.edit} className="button-primary">Edit</button>
<button onClick={this.remove} className="button-danger">Remove</button>
</div>
);
}
renderForm(){
return (
<div className="commentContainer">
<textarea ref="newText" defaultValue={this.props.children}></textarea>
<button onClick={this.save} className="button-success">Save</button>
</div>
);
}
render(){
if(this.state.editing){
return this.renderForm();
} else {
return this.renderNormal();
}
}
}
class Board extends React.Component{
constructor(){
super();
this.state = {
comments: [
"Hiya",
"Awk well",
"Boo-urns"
],
}
}
removeComment(i){
console.log("Removing comment: " + i);
var arr = this.state.comments;
arr.splice(i, 1);
this.setState({
comments: arr
});
}
updateComment(newText, i){
console.log("Updating comment: " + i);
var arr = this.state.comments;
arr[i] = newText;
this.setState({
comments: arr,
});
}
eachComment(text, i){
return (
<Comment key={i} index={i}
updateCommentText={()=>this.updateComment}
deleteFromBoard={()=>this.removeComment}>
{text}
</Comment>
);
}
render(){
return (
<div className="board">
{
this.state.comments.map(this.eachComment)
}
</div>
);
}
}
// ========================================
ReactDOM.render(
<Board />,
document.getElementById('container')
);
I'm assuming something has went out of scope but I'm not sure where. 我以为有些事情超出了范围,但我不确定在哪里。
This is one issue, but there may be more :) 这是一个问题,但可能还有更多:)
In this code: 在此代码中:
updateCommentText={()=>this.updateComment}
your arrow function returns a function reference, but does not call the function when updateCommentText
is invoked. 您的箭头函数返回一个函数引用,但在调用 updateCommentText
时不调用该函数。
Try this instead: 尝试以下方法:
updateCommentText={(value, index)=>this.updateComment(value, index)}
BTW, you have a similar issue here: 顺便说一句,您在这里有一个类似的问题:
deleteFromBoard={()=>this.removeComment}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.