godfather_satyajeet
godfather_satyajeet

Reputation: 93

having difficulty in debugging reducers in redux

I came across these reducers in the codebase I am working on at my job.

const ACTION_HANDLERS = {
  [LOGIN_REQUEST]: (state, action) => ({
    ...state,
    isAuthenticating: true
  }),
  [LOGIN_SUCCESS]: (state, action) => ({
    ...state,
    isAuthenticating: false,
    isAuthenticated: true,
    userId: action.userId,
    authToken: action.auth,
    authTTL: action.ttl,
    authCreatedAt: action.created,
    isNewUser: action.isNewUserFlag
  }),
};

export default function authReducer(state = initialAuthState, action) {
  const handler = ACTION_HANDLERS[action.type];
  if(handler!==undefined){
      console.log('login handler',handler);
     // debugger;
  }

  return handler ? handler(state, action) : state;
}

My concern is related to how to debug this method of pre-written reducers. I introduced console logs before every [] in ACTION_HANDLERS but they are syntactically wrong. I had written reducers before and they were like this.

export default function FundsReducer(state=INITIAL_STATE,action={}){

  switch(action.type){

      case GET_ALL_FUNDS_FAILED:{
        return{
          ...state,
           funds:{
            ...state.funds,
            failed:true,
            pending:false,

          }
        };
      }
      case GET_ALL_FUNDS_PENDING:
      {
        let {options}=action.payload;

        return{
          ...state,
           funds:{
            ...state.funds,
            data:[],
            failed:null,
            pending:true,
          }
        };
      }
      case GET_ALL_FUNDS:
      {
         let data;
         data=action.payload.response.data;
        return{
          ...state,
          funds:{
            ...state.funds,
            data,
            pending:false,
          }
        }
      }

I am having difficulty in debugging these reducers and introducing console logs .

Upvotes: 2

Views: 2294

Answers (1)

Atul
Atul

Reputation: 3423

You can use redux middleware as mentioned by @remix23 or just change your action as below so you will able to log state or action.

[LOGIN_REQUEST]: (state, action) => {
 console.log(action);
 return {
    ...state,
    isAuthenticating: true
  }
}

Hope this will help you.

Upvotes: 2

Related Questions