【发布时间】:2017-07-03 15:43:38
【问题描述】:
我正在使用一个 onClick 函数并尝试用它传递一个对象。我需要该对象来使该功能正常工作。但由于某种原因,它给了我这个错误。
错误来自我在每个评论上呈现的删除按钮的 onClick 函数。如果我删除我传递的评论,错误就会消失(所以函数本身不是问题)。我不确定我是通过错误的方式传递评论还是什么。
正如我通常所做的那样,如果您请求更多信息或希望我尝试控制台记录某些内容,我会将其放入编辑中,这样 cmets 就不会变得拥挤,并且其他人会更容易看到。
renderCommentsButtons(comment) {
const { post, user, auth } = this.props;
if(!user) {
return (<div></div>);
}
if(auth) {
if(user._id === comment.author.id) {
return (
<div>
<button
onClick={this.deleteComment, comment}
className="btn btn-xs btn-danger">
Delete
</button>
<Link
to={`/posts/${post._id}/comments/${comment._id}/edit`}
className="btn btn-xs btn-warning">
Edit
</Link>
</div>
)
}
}
}
renderComments() {
const { post } = this.props;
return post.comments.map((comment) => {
return (
<li className="list-group-item" key={comment._id}>
<div>
{comment.text} : {comment.author.email}
</div>
{this.renderCommentsButtons(comment)}
</li>
);
});
}
deleteComment({ author, _id}) {
const {id} = this.props.match.params;
const {user} = this.props;
if(this.props.auth) {
if(user._id === author.id){
this.props.deleteComments(id, _id, () => {
this.props.history.push(`/posts/${id}`);
});
}
}
}
【问题讨论】:
标签: javascript reactjs redux