如何在点击时获得随机数组元素的值?

时间:2019-06-06 20:18:01

标签: reactjs

board数组包含4个数字,其中之一将被随机更改为数字1,我​​需要在单击时获取该数字的值。 每个板编号有4个按钮。

class GameContainer extends React.Component {
  state = {
    board: [0, 0, 0, 0]
  };

  generateBoard = () => {
    const newBoard = this.state.board;
    const random = Math.floor(Math.random() * 4);
    newBoard[random] = 1;
    return newBoard;
  };
  findWinner = () => {

  };

  componentWillMount = () => {
    const board = this.generateBoard();
    console.log("BOARD:", board);
    this.props.createBoard(board);
    console.log("Board created");
  };

  onClick = event => {
    console.log("Click received!", event.target.id);
    this.findWinner();
  };

1 个答案:

答案 0 :(得分:1)

也许通过董事会的指标来获得价值?

https://codesandbox.io/s/nervous-edison-xmz31