Sagun Panthi
Sagun Panthi

Reputation: 47

react.js TicTakToe program

I went through the tutorial in Reactjs.org. I am stuck with the lines of code there.

which looks like this:

class Game extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      history: [
        {
          squares: Array(9).fill(null)
        }
      ],
      stepNumber: 0,
      xIsNext: true
    };
  }

  handleClick(i) {
    const history = this.state.history.slice(0, this.state.stepNumber + 1);
    const current = history[history.length - 1];
    const squares = current.squares.slice();
    if (calculateWinner(squares) || squares[i]) {
      return;
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/0.14.0/react-dom.min.js"></script>

I want to know whether const history= this.state.history access value of history from constructor history or not?

And can you explain the line of code in const current= history[history.length-1] why is this line there in the code?

Upvotes: 0

Views: 51

Answers (1)

fsl
fsl

Reputation: 3280

You could simply console.log(history, current) and find out for yourself. But to answer your questions:

1) Yes.

2) It simply gets the last history element.

Upvotes: 1

Related Questions