无法在 redux store 中设置状态

Can't set state in redux store

每次调用 reducer 时,我都会尝试将 10 添加到计数器中。我总是出错 [未处理的承诺拒绝:TypeError:null 不是对象(正在评估 'state.loaded')]

      var initialState = {
        loaded: 10
      };
      const setRandomArray = (state = initialState, action) => {
      switch (action.type) {
        case "SETARRAY":
          return {
            ...state,
            fbArray: action.fbArray
          };
        case "CLEARARRAY":
          return {
            ...state,
            fbArray: []
          };
        case "VALUETOLOAD":
          return {
            ...state,
            counter: state.loaded + 10
          };

        default:
          return null;
      }
    };

    export default setRandomArray;

我这样调用reducer:

const getRandomPictures = async () => {
    store.dispatch({ type: "VALUETOLOAD" });
};

我认为问题出在你的 switch case 上,你的 reducer returns null in the default case 这使得你的状态对象为 null,尝试 return 状态本身。