#Updating Tic-Tac-Toe Board with useReducer

1 messages · Page 1 of 1 (latest)

cold quest
#

Hey everybody, I'm a bit stuck trying to figure out how to update my Tic Tac Toe game here with useReducer. This is my first time using the hook and while I understand it a bit more now, I just can't quite figure this out. Currently I have the initial state set up with an Array that has 9 null values. In the reducer function seen on the right, I'm trying to create a new grid that takes in the current state and returns the updated grid when it is clicked. I know the current function returning newSquares isn't going to change anything, but I'm not sure how to A)pick out the grid square that was clicked and B)then give it an X or an O. I'm assuming filter method will be needed at least?

robust socket
#

You’ve almost got it. The action object has action.type and action.payload the latter contains your id. So you would need to set the id to your state e.g.:

const copyofstate = state
copyofstate[action.payload] = true