コンポーネント内の関数でthis.state
にアクセスする際に問題があります。私はすでにSO上this質問を発見し、私のコンストラクタに提案されたコードを追加しました:それはまだ動作しませんReact: 'this.state'はコンポーネント関数内では定義されていません
class Game extends React.Component {
constructor(props){
super(props);
...
this.state = {uid: '', currentTable : '', currentRound : 10, deck : sortedDeck};
this.dealNewHand = this.dealNewHand.bind(this);
this.getCardsForRound = this.getCardsForRound.bind(this);
this.shuffle = this.shuffle.bind(this);
}
// error thrown in this function
dealNewHand(){
var allCardsForThisRound = this.getCardsForRound(this.state.currentRound);
}
getCardsForRound(cardsPerPerson){
var shuffledDeck = this.shuffle(sortedDeck);
var cardsForThisRound = [];
for(var i = 0; i < cardsPerPerson * 4; i++){
cardsForThisRound.push(shuffledDeck[i]);
}
return cardsForThisRound;
}
shuffle(array) {
...
}
...
...
。 this.state.currentRound
は未定義です。何が問題ですか?
本当にこのことから何が起こっているのかわからないので、うまく見えます。これをjsのフィドルにアップロードできますか? – JordanHendrix
私は反応コードでそれを行う方法を知らない。 – hellogoodnight
es6と反応するためのベースフィドル:https://jsfiddle.net/jhonvolkd/nrd015dm/ – JordanHendrix