在这部分中,用户可以对帖子发表评论。在服务器端检查并接收数据后,我尝试更改this.state.comments
的值,然后完成。但问题是,它没有改变组件上的评论部分。
我已经阅读了有关重新渲染的先前问题,请不要标记为重复。以下是代码:
$.post("requests.php", {
requestKey: 'newComment',
commenterUser: commenterUser,
commenterEmail: commenterEmail,
theComment: theComment,
pnum: pnum}, function(data, status, xhr){
if(status == 'success'){
if(data == 'commented'){
this.setState({
comments: data
})
}else{
}
}
});
收到的
数据
是与帖子相关的所有评论,评论部分是显示所有评论的地方。
this.setState
,因为this
将是未定义的,您应该使用箭头函数或在post之前保存此值->var that = this;
,然后在回调中使用that.setState
。 - Olivier BoisséonSubmit={this.submitComment.bind(this)}
并且state.comments
的值确实会改变。 :( - sinawic