これは私のモデルである:状態で、最近設定されたプロパティにアクセスすると、以前の値を返します
class Board extends React.Component {
constructor() {
super();
this.state = {
squares: Array(3).fill(Array(3).fill(null)),
xIsNext: true
};
}
get turn() {
return this.state.xIsNext ? 'X' : 'O';
}
handleTurn(x, y) {
const squares = this.state.squares.slice().map(function(row) {
return row.slice();
});
const turn = this.turn; // this returns either "X" or "Y"
squares[x][y] = turn; // this is the value I'm going to be accessing later on
this.setState({
squares: squares
});
if (this.isSolved(x, y)) {
console.log('VICTORY!')
}
this.nextPlayer();
}
isSolved(x, y) {
var count = 0
var player = this.state.squares[x][y]; // here is when I try to access the value set at at 'handleTurn')
// more stuff down there
return false;
}
私の問題はこれです:isSolved
はhandleTurn
から呼び出され、handleTurn
に私からの座標のいずれかを設定します二次元で 'X'または 'Y'のいずれかになります。しかし、その値をisSolved
にチェックすると、私はちょうど設定した値ではなく、の前のの値を取得します。
例えば、最初の呼び出しにnull
(私はX
を期待する場合)、2回目の呼び出しを取得し、(私はそれまでにO
を期待するとき)私はなど
グレート:
はここ
setState
の行動に、より深みに入る興味深い記事です!ありがとう。今私はドキュメントでそれを見つける。 :) – yivi