React组件值未更新

时间:2017-10-31 14:02:18

标签: reactjs

我跟着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;
}

如果有人知道为什么在点击按钮重置值后没有任何反应,我会很高兴。

1 个答案:

答案 0 :(得分:1)

您的主要问题是您分配给setState函数而不是调用它:

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

应该是

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