Javascript 错误:onClick侦听器应为函数,而不是使用reactjs获取类型对象
我正在使用一个onClick函数,并试图用它传递一个对象。我需要对象使函数工作。但出于某种原因,它给了我这个错误 错误来自我在每个注释上呈现的delete按钮的onClick函数。如果我删除了正在传递的注释,错误就会消失(因此函数本身不是问题)。我不确定我的评论是错误的还是什么 正如我通常所做的那样,如果您要求更多信息或希望我尝试控制台记录一些东西,我会将其放入编辑中,这样评论就不会变得拥挤,其他人也会更容易看到Javascript 错误:onClick侦听器应为函数,而不是使用reactjs获取类型对象,javascript,reactjs,redux,Javascript,Reactjs,Redux,我正在使用一个onClick函数,并试图用它传递一个对象。我需要对象使函数工作。但出于某种原因,它给了我这个错误 错误来自我在每个注释上呈现的delete按钮的onClick函数。如果我删除了正在传递的注释,错误就会消失(因此函数本身不是问题)。我不确定我的评论是错误的还是什么 正如我通常所做的那样,如果您要求更多信息或希望我尝试控制台记录一些东西,我会将其放入编辑中,这样评论就不会变得拥挤,其他人也会更容易看到 renderCommentsButtons(comment) {
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}`);
});
}
}
}
renderCommentsButtons(注释){
const{post,user,auth}=this.props;
如果(!用户){
返回();
}
if(auth){
if(user.\u id==comment.author.id){
返回(
删除
编辑
)
}
}
}
renderComments(){
const{post}=this.props;
返回post.comments.map((comment)=>{
返回(
{comment.text}:{comment.author.email}
{this.renderCommentsButtons(comment)}
);
});
}
deleteComment({author,_id}){
const{id}=this.props.match.params;
const{user}=this.props;
if(this.props.auth){
if(user.\u id==author.id){
this.props.deleteComments(id,_id,()=>{
this.props.history.push(`/posts/${id}`);
});
}
}
}
试试这个:
<button
onClick={() => this.deleteComment(comment)}
className="btn btn-xs btn-danger">
Delete
</button>
this.deleteComent(comment)}
className=“btn btn xs btn危险”>
删除
您没有正确地传递注释,
onClick
只接受一个函数,如果您想传递参数,您必须像上面那样进行操作。这导致它工作!为什么我不能像以前那样度过难关?我以前做过,效果很好。@TaylorAustin你错了。您以前从未这样做过,因为react事件处理程序只接受一个参数,即事件发生时要调用的函数。如果你能找到那个例子,我可以说明为什么它是不同的对不起,你能帮我解决这个问题吗?