1

As I understand, button has a click event, and the function assigned to it is handleClick. onClick is triggered when the click event happens, but it if I understand it correctly, the argument for the function, a number from 0 to 8, is already stored somewhere.

Does this have to do something with closures?

So basically I don't get, how the numbers are passed as an argument when the click event happens.

And shouldn't the i of handleClick be a parameter, that accepts an object, which describes the event that has occurred? - instead of accepting numbers in this case.

I would really appreciate, if you could help me with my confusion.

Here is the game:

https://codepen.io/gaearon/pen/LyyXgK?editors=0010

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),
      xIsNext: true,
    };
  }

  handleClick(i) {

    console.log(i) // a number from 0 to 8, instead of an Object describing the Event

    const squares = this.state.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
    }
    squares[i] = this.state.xIsNext ? 'X' : 'O';
    this.setState({
      squares: squares,
      xIsNext: !this.state.xIsNext,
    });
  }

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

  render() {
    const winner = calculateWinner(this.state.squares);
    let status;
    if (winner) {
      status = 'Winner: ' + winner;
    } else {
      status = 'Next player: ' + (this.state.xIsNext ? 'X' : 'O');
    }

    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>
      </div>
    );
  }
}

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

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

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

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 Answers1

1

If I understood you question correctly, below line is your confusion.

onClick={() => this.handleClick(i)}

OnClick is assigned with function which will be executed when it is clicked.

onClick={function()}

Now we are declaring a anonymous function in es6 syntax.

onClick={() => { ..some code to execute}}

After this we are calling a handleClick function with argument i inside that anonymous function.

onClick={() => this.handleClick(i)}

This is the flow for that code.

Now coming on to event you can write code like this.

onClick={(event) => this.handleClick(event)}

now your handleClick will have access to event and you can take value as event.target.value

Anil Kumar
  • 2,223
  • 2
  • 17
  • 22
  • I see how specifying a function parameter `i`, like `onClick={(i) => this.handleClick(i)}` gives a different result now. `i` is now an object describing the Event, which I would have expected. But what I don't understand, is how `i` has the value of a number, if we don't specify the function parameter. Specifying `i` as a parameter is basically shadowing the variable `i`, if I understand it correctly. –  Feb 11 '19 at 11:14
  • @tadadadadi `i` is a parameter in renderSquare, and inside render renderSquare is called with a number argument E.G. `this.renderSquare(2)`. – Paul Feb 11 '19 at 15:45
  • @Paulpro I see where the argument comes from, I just don't understand how. When the click event happens, `renderSquare(2)` has already been called, which means, the number value is stored somewhere, waiting for the click event to happen, or not? –  Feb 11 '19 at 17:11
  • @tadadadadi Yes, it is stored in a closure. In the same way that 2 is stored in `x` when `add(2)` is called in this answer: https://stackoverflow.com/a/18234552/772035 – Paul Feb 11 '19 at 17:19