Justin Spring
Justin Spring

Reputation: 37

Can't set state in redux store

I'm trying to add 10 to the counter each time the reducer is called. I always get en error [Unhandled promise rejection: TypeError: null is not an object (evaluating '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;

I am calling the reducer like this:

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

Upvotes: 1

Views: 337

Answers (1)

Djellal Mohamed Aniss
Djellal Mohamed Aniss

Reputation: 1733

i think the problem is with your switch case, your reducer returns null in the default case which makes your state object null, try to return the state itself.

Upvotes: 3

Related Questions