CombineReducer Name Reducer

by Victor Santos

JavaScript

//Reducers

  const nameReducer = (state, action) => {
    switch (action.type) {
      case "CHANGE_NAME":
        return { ...state, name: action.payload };
      default:
        return state;
    }
  };
  
 const countReducer = (state, action) => {
    switch (action.type) {
      case "INCREMENT_COUNT":
        return { ...state, count: state.count + action.payload.count, countAction: action.payload.countAction};
      case "DECREMENT_COUNT":
        return { ...state, count: state.count - action.payload.count, countAction: action.payload.countAction };
      default:
        return state;
    }
  };
  
  
  //State
  const initialState = {
  	nameReducer: {
    	name: ""
    },
    countReducer: {
     	count: 0,
      countAction: "NO ACTION"
    }
  }
  
  
  //Testing Name Reducer
  const actionName = (payload) => ({ type: "CHANGE_NAME", payload });
  console.log("This is my initial value: ", initialState.nameReducer.name );
  const reducer1 = nameReducer(initialState.nameReducer, actionName("John Lock"))
  console.log("This is the name value after first dispatch", reducer1);
  const reducer2 = nameReducer(initialState.nameReducer, actionName("Alex Pen"))
  console.log("This is the name value after second dispatch", reducer2);
  
  
  //Testing  Count Reducer
  const actionIncrement= (payload) => ({ type: "INCREMENT_COUNT", payload: {count: payload, countAction: "INCREMENT_COUNT" } });
  const actionDecrement= (payload) => ({ type: "DECREMENT_COUNT", payload: {count: payload, countAction: "DECREMENT_COUNT" } });
    
    //CONSOLE PRINT
  console.log("This is my initial value: ", initialState.countReducer.count, " ACTION: ", initialState.countReducer.countAction  );
  
  //We change the initial state and this method return a new state calle v
  const countReducerState1 = countReducer(initialState.countReducer, actionIncrement(2))
  console.log("This is the value after dispatch: ", countReducerState1 );
  
  //we take the new state and dispach another...