1

tic-tac-toe game:

X | O | O | X | X | O | X 

I try to learn ReactJS. I already follow tutorial from https://react.dev/learn/tutorial-tic-tac-toe. But had a hard time to solve the 2nd Challenge of the tutorial.

This is a Square Component:

function Square({ value, onSquareClick }) { return ( <button className="square" onClick={onSquareClick}> {value} </button> ); } 

So, we have main board of the tic-tac-toe which generated manually. It contains of 9 Square components.

<> <div className="board-row"> <Square value={squares[0]} onSquareClick={() => handleClick(0)} /> <Square value={squares[1]} onSquareClick={() => handleClick(1)} /> <Square value={squares[2]} onSquareClick={() => handleClick(2)} /> </div> <div className="board-row"> <Square value={squares[3]} onSquareClick={() => handleClick(3)} /> <Square value={squares[4]} onSquareClick={() => handleClick(4)} /> <Square value={squares[5]} onSquareClick={() => handleClick(5)} /> </div> <div className="board-row"> <Square value={squares[6]} onSquareClick={() => handleClick(6)} /> <Square value={squares[7]} onSquareClick={() => handleClick(7)} /> <Square value={squares[8]} onSquareClick={() => handleClick(8)} /> </div> </> </> 

It works great, the expected behavior is: when I click one of the squares for the first time, the clicked square user interface is changed into 'X'. It works when the board generated manually.

The issue comes in 2nd challenge, when the square components are generated automatically.

I use Array.Map and the UI of 9 squares are successfully generated. But, when I click one of the square for the first time, all the squares that automatically generated changed into 'X'.

function generateSquare(dimension) { let dimensionTranslate = [...Array(dimension).keys()]; return( dimensionTranslate.map((row) => { return(<div className="board-row"> { dimensionTranslate.map((column) => { let squareIndex = (dimension*row)-row + column; return <Square key={squareIndex} value={squares[{ squareIndex }]} onSquareClick={() => handleClick({ squareIndex })} /> }) } </div> ) }) ) } 

Please see the picture of the Problem

PS:

  1. The square Index generating correct value :0,1,2,3,4,5,6,7,8.
  2. This code (Generating Square Components Automatically) is in the same code-block/same function/same Component(Board Component, parent of Square Component) where I generate 9 square Components Manually

Meanwhile, despite of the error in automatic generated, the manual generated Square still working like usual.

I already working on this for almost 2 days, but I couldn't found the root cause of the issue.

I already tried following things :

  1. Change the map into for loop (Doesn't work)
  2. Reduce the looping of .Map Function, currently it has 2 loop for making new row in user interface, I make only 1 line ,1.map function, I thought the reason is because of multiple map. (Doesn't work, the behavior still the same.)
  3. Make sure the Squareindex is correctly assigned (It is assigned correctly, Doesn't work, the behavior still the same.)
  4. Put the the Mapping result in Const variable instead of a function with a return (Doesn't work, the behavior still the same.)

Please check the sandbox here: https://codesandbox.io/p/sandbox/tic-tac-toe-tutorial-mpvcr7

9
  • You can pass the index argument to your map functions, it'll be easier : stackoverflow.com/a/38364482/12927815 Also I think you should use "const" when assigning the squareIndex value, not sure if it'll change anything but it's worth a try. Commented May 4, 2024 at 9:04
  • Hi @ArnaudFlaesch, thank you for your response. I appreciate it. Actually, the array itself , the dimensionTranslate, just to mimic the way I could use Array.Map to generate Square Component. Indeed, I already tried your suggestion along the way when I was debugging. But I realize it doesn't matter. I could use either the index or the value, because the value = index in this case. Commented May 4, 2024 at 9:11
  • can you please share your code sandbox? Commented May 4, 2024 at 9:14
  • 2
    handleClick({ squareIndex }); is one of several issues in your code. It's supposed to be handleClick(squareIndex);. I would strongly suggest you learn TypeScript. Commented May 4, 2024 at 9:32
  • 1
    Upvoted the comment above, you're receiving an object and treating it as a number. When I update the code on the sandbox with a fix, the second board is updated correctly after each move. Commented May 4, 2024 at 9:39

1 Answer 1

0

This ticket can be closed. The issue is on handleClick( {squareIndex} ) rather than handleClick(squareIndex) as mentioned by @super. Thank you everyone

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.