简体   繁体   English

React-Redux:尝试调度将嵌套数组分配给另一个数组的操作时,挂钩调用无效

[英]React-Redux:Invalid hook call while trying to dispatch an action assigning a nested array with another one

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.这是我使用 React-Redux 的第一个项目。我试图在调度 moveDown() 操作时将从 MoveDown() function 返回的数组分配给 state 操作中的数组。

Action:行动:

export const moveDown = () => {
    return {
    type: MOVE_DOWN,
    };
};

Here is the function called everytime I press a specific button:这是每次按下特定按钮时调用的 function:

  const Press = () => {
       dispatch(moveDown());
  };

Initial state:初始 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:我调用的减速器和 function 是为了更改返回另一个数组的数组:

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;
 };

Code looks pointless, as you are using useSelector hook inside reducer... useSelector is a hook to use inside component to get a faster access to state values as an alternative to mapStateToProps .代码看起来毫无意义,因为您在减速器中使用useSelector钩子... useSelector是一个钩子,用于在组件内部使用以更快地访问 state 值作为mapStateToProps的替代方法。 In your case it is enough to pass state value to MoveDown function as parameter and read it directly在您的情况下,将 state 值传递给MoveDown function 作为参数并直接读取就足够了

export const board = (state = initialState.board, action) => {
       if (action.type == MOVE_DOWN) {
              return {
                     ...state,
                     arr: MoveDown(state),
              };
       }
       return state;
};
const MoveDown = state => {
        const mat = state.arr;
        const size = state.gridSize;
        // ...etc

But your function has another problems you will encounter, for example this loop will not work但是你的 function 还有另一个问题你会遇到,例如这个循环不起作用

for (var i = size - 2; i < 0; i--) 

because second parameter is a condition for loop to work, it should be most probably i >= 0 , otherwise ot it will never start if size - 2 is positive number, or it will never end if is negative因为第二个参数是循环工作的条件,它很可能应该是i >= 0 ,否则如果 size - 2 是正数,它将永远不会开始,如果是负数,它将永远不会结束

PS Also js code style prescribes to use camel case for function names so it should not start from capital M PS 此外,js 代码样式规定对 function 名称使用驼峰式大小写,因此不应从大写 M 开始

React hooks should be used only inside react function component or another react hook.反应钩子应该只在反应 function 组件或另一个反应钩子内部使用。

useSelector is a react hook. useSelector是一个反应钩子。 But you are calling it from a normal JavaScript function.但是您从普通的 JavaScript function 调用它。

I have modified your code, feel free to give it a try我已经修改了你的代码,你可以试试

export const board = (state = initialState.board, action) => {
       if (action.type == MOVE_DOWN) {
              return {
                     ...state,
                     arr: MoveDown(state),
              };
       }
       return state;
};
const MoveDown = (boardState) => {
        const mat = boardState.arr;
        const size = boardState.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;
 };

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM