Reactjs 将函数绑定到React中的当前项
我想显示一个按钮来添加一本书,如果该书不存在于usersBooks数组中,我认为该函数运行良好,但绑定会导致问题:Reactjs 将函数绑定到React中的当前项,reactjs,function,find,bind,Reactjs,Function,Find,Bind,我想显示一个按钮来添加一本书,如果该书不存在于usersBooks数组中,我认为该函数运行良好,但绑定会导致问题: doesExistInUsersBooks = (title) => { if(this.props.userBooks.find(b => b.title == title)) { return true; } else { return false; } } 我
doesExistInUsersBooks = (title) => {
if(this.props.userBooks.find(b => b.title == title)) {
return true;
} else {
return false;
}
}
我这样使用它:
!this.doesExistInUserBooks.bind(this,title)&&
声明函数后,您正在使用ES6自动绑定。这就是为什么不需要绑定。您应该尝试将代码更改为:
!!DoeSistinUserBooks(标题)您需要调用函数:
this.doesxistinusersbooks.bind(this,title)(&&
在那里检查它,它正在工作:
您可以这样声明您的函数:
doesExistInUsersBooks(title){
// your code
}
或者如Serdar所建议的那样使用它:
!doesExistInUserBooks(title)
在构造函数中绑定它:
this.doesistinuserbooks=this.doesistinuserbooks.Bind(this)
在您不再需要绑定它之后,我知道,我使用es6,但仍然需要绑定创建网络请求的函数,在此之前,它已经为数组中的每个项创建了10个请求。。