這是初學者的React.js/ES2015問題。如何訪問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)和愈傷組織ng object(this.defaultValue)?
要麼這是調用對象(文本字段,因爲它是目前),或者我可以在類的構造函數綁定這到類
this.onBookNameChange = this.onBookNameChange.bind(this);
但後來我失去的文本字段訪問。我看不到在方法中獲取對這兩個對象的引用的任何方法。
其實,更好的使用[ 'event.currentTarget'](https://developer.mozilla.org/en-US/docs/Web/API/Event/currentTarget) – Bergi
@Bergi:是的,如果你想要一個一個換一個這個本來會是什麼的好點。 –