2017-10-31 17 views
0

私はthis tutorialの後にReactでtic tac toeサンプルアプリケーションを作成しました。反応成分の値が更新されていません

function Square(props) { 
    return (
    <button className="square" onClick={props.onClick}> 
     {props.value} 
    </button> 
); 
} 

class Board extends React.Component { 
    constructor(props) { 
    super(props); 
    this.state = { 
     squares: Array(9).fill(null), 
     playerIdx: 0, 
     finished : false, 
    }; 

    this.players = [ 
     createPlayer("lucas", "X"), 
     createPlayer("livia", "O"), 
    ]; 

    this.resetGame = this.resetGame.bind(this); 
    } 

    handleClick(i) { 
    const squares = this.state.squares.slice(); 
    if (calculateWinner(squares) || squares[i]) { 
     return; 
    } 

    squares[i] = this.players[this.state.playerIdx].value; 
    this.setState({ 
     squares: squares, 
     playerIdx: (this.state.playerIdx + 1) % 2, 
    }); 
    } 

    resetGame(){ 
    const players = this.players.slice(); 
    players[0] = createPlayer(this.players[1].name, "X"); 
    players[1] = createPlayer(this.players[0].name, "O"); 

    this.setPlayers = (players); 
    this.setState = ({ 
     squares: Array(9).fill(null), 
     playerIdx: 0, 
    }); 
    } 

    renderSquare(i) { 
    return <Square 
      value={this.state.squares[i]} 
      onClick={() => this.handleClick(i)} 
      />; 
    } 

    render() { 
    const winner = calculateWinner(this.state.squares); 
    let status; 
    if (winner) { 
     this.state.finished = true; 
     status = 'Winner: ' + this.players[this.state.playerIdx - 1].name; 
    } else { 
     status = 'Next player: ' + this.players[this.state.playerIdx].name; 
    } 

    return (
     <div> 
     <div className="status">{status}</div> 
     <div className="board-row"> 
      {this.renderSquare(0)} 
      {this.renderSquare(1)} 
      {this.renderSquare(2)} 
     </div> 
     <div className="board-row"> 
      {this.renderSquare(3)} 
      {this.renderSquare(4)} 
      {this.renderSquare(5)} 
     </div> 
     <div className="board-row"> 
      {this.renderSquare(6)} 
      {this.renderSquare(7)} 
      {this.renderSquare(8)} 
     </div> 
     { this.state.finished && 
      <button onClick={() => this.resetGame(this)}> Reset </button> 
     } 
     </div> 
    ); 
    } 
} 

class Game extends React.Component { 
    render() { 
    return (
     <div className="game"> 
     <div className="game-board"> 
      <Board /> 
     </div> 
     </div> 
    ); 
    } 
} 

// ======================================== 

ReactDOM.render(
    <Game />, 
    document.getElementById('root') 
); 

function createPlayer(name, value){ 
    return { 
    name: name, 
    value: value 
    }; 
} 

function calculateWinner(squares) { 
    const lines = [ 
    [0, 1, 2], 
    [3, 4, 5], 
    [6, 7, 8], 
    [0, 3, 6], 
    [1, 4, 7], 
    [2, 5, 8], 
    [0, 4, 8], 
    [2, 4, 6], 
    ]; 
    for (let i = 0; i < lines.length; i++) { 
    const [a, b, c] = lines[i]; 
    if (squares[a] && squares[a] === squares[b] && squares[a] === squares[c]) { 
     return squares[a]; 
    } 
    } 
    return null; 
} 

誰もが、私は値をリセットするためのボタンをクリックした後、何も起こらない理由は、私は本当に喜んでいるでしょう知っている場合: は、だから私は、ゲームにプレイヤーを追加するためにいくつかの変更をしたコードは次のようです。

+0

である必要があり、それはhandleClickが正常に動作していないということですか? –

+0

あなたはonClickで "this"をバインドしようとしましたか?onClick = {()=> this.resetGame.bind(this)} ' – Aaqib

+1

コンストラクタ@Aaqibにすでに入っています –

答えて

1

あなたの主な問題は、あなたがsetState機能にを割り当てる代わりに、それを呼び出しているということです。

this.setState = ({ 
    squares: Array(9).fill(null), 
    playerIdx: 0, 
}); 

this.setState({ 
    squares: Array(9).fill(null), 
    playerIdx: 0, 
}); 
+0

それは働いた。ありがとう ;) –

関連する問題