这是一个初学者 React.js / ES2015 问题。
我有一些显示书名列表的 HTML:
class BookList extends React.Component
{
render()
{
return (
<div>
{this.state.books.map((book) =>
{
return (
<TextField
defaultValue={book.name}
key={book.name}
onEnterKeyDown={this.onBookNameChange}
...
当用户编辑一个名字并按下回车键时,我会在我的类中调用一个方法来检查这本书是否存在并更新我的商店:
class BookList extends React.Component
{
onBookNameChange(event)
{
const newBookName = event.target.value;
if (this.doesBookExistElseShowMessage(newBookName))
return;
const oldName = this.defaultValue;
const oldBooks = Store.getState().books;
const newBooks = Helper.EditValueInArray(oldBooks, 'name', oldName, newBookName);
Actions.updateBooks(newBooks);
}
问题是:在这种方法中,我如何访问类(this.doesBookExistElseShowMessage)和调用对象(this.defaultValue)?
这是调用对象(TextField,目前是),或者我可以在类构造函数中将它绑定到类
this.onBookNameChange = this.onBookNameChange.bind(this);
但后来我失去了对 TextField 的访问权限。我看不到任何方法可以在方法中获取对这两个对象的引用。