1

board array contains 4 numbers, one of them will be randomly changed to the number 1, i need to get the value of that number on click. There are 4 buttons for each board numbers.

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();
  };
2
  • 2
    Store random somewhere (state)? Hard to say without having a complete example. Commented Jun 6, 2019 at 20:22
  • you need to setState the new board somewhere? Commented Jun 6, 2019 at 20:23

1 Answer 1

1

Maybe get value by board's index ?

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.