3
如何在该组件函数中获取React组件状态。这没有与状态相关的对象。我得到这个。状态未定义在板removeComment
函数。基本上在董事会removeComment
函数我想删除该索引的评论元素(作为参数传递)。无法访问事件处理程序函数中的`this.state`:ReactJS
class Board extends React.Component {
constructor(props) {
super(props);
this.state = {
comments:[
"One",
"Two",
"Three",
"four",
"five"
]};
};
removeComment(index) {
console.log('i was called',this.state);
}
render() {
console.log(this.state);
return (
<div className="board">
{
this.state.comments.map((text,i) => {
return (
<Comment key ={i} index = {i} commentText={text} removeComment={this.removeComment}/>
)
})
}
</div>
);
}
}
class Comment extends React.Component {
removeComment() {
var index = this.props.index;
this.props.removeComment(index);
}
render() {
return(
<div onClick={this.removeComment.bind(this)}>{this.props.commentText}</div>
);
}
}
感谢花花公子。你拯救了我的一天。 –
很高兴,帮助你:) –