Reactjs 未捕获类型错误:this.updateSelf不是函数
所以我有一个小的开始项目 我将一个方法从一个组件移到了顶部的应用程序组件中,现在当我调用主应用程序组件中的另一个方法时,它会说它不存在,这让我非常困惑Reactjs 未捕获类型错误:this.updateSelf不是函数,reactjs,Reactjs,所以我有一个小的开始项目 我将一个方法从一个组件移到了顶部的应用程序组件中,现在当我调用主应用程序组件中的另一个方法时,它会说它不存在,这让我非常困惑 selectStateUpdate(book,shelf) { console.log("Sel"+JSON.stringify(book)+shelf) this.updateShelf(book, shelf); } updateShelf = (book, shelf) => { console.log("Updatin
selectStateUpdate(book,shelf) {
console.log("Sel"+JSON.stringify(book)+shelf)
this.updateShelf(book, shelf);
}
updateShelf = (book, shelf) => {
console.log("Updating shelf")
BooksAPI.update(book, shelf)
.then(() => BooksAPI.getAll())
.then((books) => {
this.setState({books})
})
}
上面您可以看到我正试图通过this.updateself调用updateself。有关完整上下文,请参见
运行该项目时,当该函数尝试运行时,控制台中出现以下错误
Uncaught TypeError: this.updateShelf is not a function
at Object.selectStateUpdate [as updateSelect] (App.js:21)
对于完整上下文,可以查看项目
非常感谢任何帮助,或者如果您需要任何澄清,请询问。将您的
选择状态更新(书籍,书架)
更改为选择状态更新=(书籍,书架)=>{}
希望这会有帮助 大多数时候
这个。不是功能错误
由于在错误的上下文中使用“this”而发生,因此您可以执行这些解决方案之一。此时,您可以调用selectStateUpdate来执行此操作
this.selectStateUpdate(book,shelf).bind(this);
或者使用这种语法
selectStateUpdate = (book, shelf) => {
console.log("Sel" + JSON.stringify(book) + shelf);
this.updateShelf(book, shelf);
};