我有一个供稿,您可以在其中发布条目。然后,任何用户都可以喜欢这些条目。问题是,在您喜欢一个条目并继续编写自己的条目之后(从而将新条目推到提要的顶部),您单击的所有类似按钮的状态都将在视觉上撤消,以便查看显示的正确数据需要手动刷新。
这个问题非常奇特,不幸的是我没有发现任何可能与此相关的东西。
这是我的 Like 组件代码(like.es6.jsx):
class Like extends React.Component {
constructor(props){
super(props);
this.state={
like: this.props.entry.liked,
likes: this.props.entry.likes_count
}
this.post = this.post.bind(this);
this.delete = this.delete.bind(this);
}
post () {
if(!(this.state.like)){
$.ajax({
method: "POST",
url: "/api/v1/likes",
data: {
log_entry_id: this.props.entry.id
},
})
.then( (response) => {
this.setState({like: true})
let likes = this.state.likes;
likes++;
this.setState({likes: likes})
});
}
}
delete(){
if(this.state.like){
$.ajax({
method: "DELETE",
url: "/api/v1/likes/" + this.props.entry.id
})
.then( (response) => {
this.setState({like: false})
let likes = this.state.likes;
likes--;
this.setState({likes: likes})
});
}
}
render () {
let show_likes = "";
if(this.state.like) {
show_likes = (this.state.likes > 1) ? ("You and " + (this.state.likes - 1) + ((this.state.likes == 2) ? (" other person") : (" others" )) + (" like this.")) : ("You like this.");
}else if(this.state.likes > 0){
show_likes = (this.state.likes == 1) ? (this.state.likes + " person likes this.") : (this.state.likes + " people like this.");
}
let like_button = this.state.like ?
<a onClick={this.delete} className=" pull-right"><i className="fa fa-thumbs-o-down"></i> Dislike</a> :
<a onClick={this.post} className=" pull-right"><i className="fa fa-thumbs-o-up"></i> Like</a>
return (
<div>
<span>
{show_likes}
</span>
{like_button}
</div>
);
}
}
在父组件“log_entry.es6.jsx”中,我通过以下方式添加了类似的组件:
<Like entry={entry} key={entry.id} />
如果我可以提供更多信息,我会很乐意应要求提供。感谢您花时间审查我的问题!