This is my first project using React-Redux.I am trying to assign the array which is returned from the MoveDown() function to the array in the state when dispatching the moveDown() action.
Action:
export const moveDown = () => {
return {
type: MOVE_DOWN,
};
};
Here is the function called everytime I press a specific button :
const Press = () => {
dispatch(moveDown());
};
Initial state:
export const initialState = {
board: {
gridSize: 4,
arr: [
[0, 0, 0, 0],
[0, 2, 4, 0],
[0, 0, 16, 2],
[0, 2, 32, 0],
],
},
};
And the reducer and function that I call in order to change the array which returns another array:
export const board = (state = initialState.board, action) => {
if (action.type == MOVE_DOWN) {
return {
...state,
arr: MoveDown(),
};
}
return state;
};
const MoveDown = () => {
const mat = useSelector((state) => state.board.arr);
const size = useSelector((state) => state.board.gridSize);
var ok = 1;
for (var j = 0; j < size; j++) {
while (ok) {
ok = 0;
for (var i = size - 2; i < 0; i--) {
if (mat[i][j] != 0) {
if (mat[i + 1][j] == mat[i][j]) {
mat[i][j] = 0;
mat[i + 1][j] = 2 * mat[i + 1][j];
ok = 1;
} else {
if (mat[i + 1][j] == 0) {
mat[i + 1][j] = mat[i][j];
ok = 1;
}
}
}
}
}
}
return mat;
};
question from:
https://stackoverflow.com/questions/65540691/react-reduxinvalid-hook-call-while-trying-to-dispatch-an-action-assigning-a-nes 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…