class Game extends React.Component { constructor(props) { super(props); this.state = { history: [ { squares: Array(9).fill(null), location : 0 // Here I have introduced location to hold index } ], stepNumber: 0, xIsNext: true }; }
handleClick(i) { const history = this.state.history.slice(0, this.state.stepNumber + 1); const current = history[history.length - 1]; const squares = current.squares.slice(); if (calculateWinner(squares) || squares[i]) { trở lại; } squares[i] = this.state.xIsNext ? "X" : "O"; this.setState({ history: history.concat([ { squares: squares, location : i // Assigning Index } ]), stepNumber: history.length, xIsNext: !this.state.xIsNext }); }
đóng cửa. Câu hỏi này cần được hỏi tập trung hơn. Câu trả lời không được chấp nhận vào thời điểm này. Bạn muốn cải thiện vấn đề này? Đã cập nhật câu hỏi để chỉ tập trung vào một vấn đề chỉnh sửa bài đăng này. Đã đóng 8 năm trước. Cải thiện truy vấn này
Tôi là một lập trình viên xuất sắc, rất giỏi!