Ho problemi ad accedere a this.state
in funzioni all'interno del mio componente. Ho già trovato this domanda su SO e aggiunto il codice suggerito al mio costruttore:React: "this.state" non è definito all'interno di una funzione componente
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) {
...
}
...
...
E ancora non funziona. this.state.currentRound
non è definito. Qual è il problema?
non posso davvero dire che cosa sta succedendo da questo, sembra a posto. Puoi caricare questo in un js fiddle? – JordanHendrix
Non so come farlo con codice di reazione. – hellogoodnight
base fiddle per es6 e reagire: https://jsfiddle.net/jhonvolkd/nrd015dm/ – JordanHendrix