Skip to content Skip to sidebar Skip to footer

Redux: Using Spread Operator On 2d Array

New to React.js, I am having hard time using the spread operator in my reducers to update my state that has an 2D-array property. For instance initial state is so: let initialState

Solution 1:

its because the old value is assigned to null

let initialState = { grid: new Array(5).fill(new Array(5).fill(0)), player: { coords: null, health: 100 } }

and then you are trying to read zeroth index of null, that will throw the error.

let oldCoords = [state.player.coords[0], state.player.coords[1]];

Update:

grid is an array but you are returning the object ..change to below syntax

grid: [ ...state.grid, state.grid[oldCoords[0]][oldCoords[1]]=1 ]   

Post a Comment for "Redux: Using Spread Operator On 2d Array"