[英]Binding function to current item in React
如果在usersBooks數組中不存在該書,我想顯示按鈕以添加一本書,我認為該函數運行良好,但綁定會導致問題:
doesExistInUsersBooks = (title) => {
if(this.props.userBooks.find(b => b.title == title)) {
return true;
} else {
return false;
}
}
我用這種方式: !this.doesExistInUserBooks.bind(this, title) && <Button ... />
聲明函數后,您正在使用ES6自動綁定。 這就是為什么不需要綁定。 您應該嘗試將代碼更改為此:
!doesExistInUserBooks(title)
您需要致電函子:
this.doesExistInUsersBooks.bind(this, title)() && <Button ... />
檢查它是否正常工作: https : //stackblitz.com/edit/react-xedawq?embed=1&file=index.js
您可以做的就是以這種方式聲明您的函數:
doesExistInUsersBooks(title){
// your code
}
或如Serdar建議的那樣使用它:
!doesExistInUserBooks(title)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.