无法在Redux存储中设置状态

时间:2019-02-21 18:31:04

标签: reactjs redux react-redux react-state-management

我试图在每次调用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;

我这样称呼减速器:

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

1 个答案:

答案 0 :(得分:3)

我认为问题出在开关情况下,化简器在 default 情况下返回null,这会使您的状态对象为null,请尝试返回状态本身。