0

Consider the following reducer:

const initialBaseTestState = {
  oldValue: "String",
  something: "String two",
};

const baseTest = produce((state = initialBaseTestState, action) => {
const { type, data } = action;
switch (type) {
 case SET_VALUE: {
  state.newValue = "test";
  sendUpdatedState(original(state)); // Send updated state  
  return state;
}

Code of function sendUpdatedState:

export const updateMonthlyTotalPrice = updatedState => {
  const dispatch = useDispatch();
  console.log(updatedState); 
  // Getting: {oldValue: "String", something: "String two"} 
  // Requiring: {oldValue: "String", something: "String two", newValue: "test"} 
  const string = updatedState.newValue + "s";  
  dispatch(setSomeValueAction(string));
};

I know when the original is called with 'old' state, so why I am getting the initialBaseTestState. Is it some approach to get already updated state after the state.newValue is updated in produce.

NOTE: I would like to keep using the Immer

M. Falex
  • 79
  • 2
  • 8

1 Answers1

0

Solution can be:

      const originalState = original(state);
      const newState = {
        ...originalState,
           newValue: "test"
        },
      };

Is a better way how to do it?

M. Falex
  • 79
  • 2
  • 8