Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
273 views
in Technique[技术] by (71.8m points)

javascript - 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.

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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. In your case it is enough to pass state value to MoveDown function as parameter and read it directly

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

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

PS Also js code style prescribes to use camel case for function names so it should not start from capital M


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...