hitsla.blogg.se

React course
React course





Now `squares` is unchanged, but `nextSquares` first element is 'X' rather than `null`

react course

In the end the user sees that the upper left square has changed from empty to having a X after clicking it. This causes the value prop of the Square component with index 0 to change from null to X.

  • The squares state of the Board component was updated, so the Board and all of its children re-render.
  • handleClick uses the argument ( 0) to update the first element of the squares array from null to X.
  • It calls handleClick with an argument of 0. The Board component defined that function directly in the JSX. The Square component received that function as its onSquareClick prop from the Board.
  • Clicking on the upper left square runs the function that the button received as its onClick prop from the Square.
  • Let’s recap what happens when a user clicks the top left square on your board to add an X to it:

    react course

    Keeping the state of all squares in the Board component will allow it to determine the winner in the future. When the Board’s state changes, both the Board component and every child Square re-renders automatically.

    react course

    When clicking on a Square, the child Square component now asks the parent Board component to update the state of the board. Now that your state handling is in the Board component, the parent Board component passes props to the child Square components so that they can be displayed correctly.







    React course