If have a state of objects payload:
export const initialState = {
payload: {
firstName: 'Mick',
lastName: 'Andri',
phoneNumber: '651-332-5675',
electronicMailAddress: 'mandradi@lake.com',
addressLine1: '27 Crest Ave',
addressLine2: '',
cityName: 'San Jose',
stateCode: 'CA',
zipCode: '92122',
},
};
and to load this state it in some way I using immer:
const SettingsReducer = (state = initialState, action) =>
produce(state, (draft) => {
switch (action.type) {
case actionTypes.LOAD_SETTINGS_INFO:
draft.payload = action.payload;
break;
}
});
export default SettingsReducer;
If we were to do this the usual way with JavaScripts object and array spread syntax, our state reducer might look like below, this is just an example:
draft.payload = action.payload;
case 'UPDATE_SETTINGS_INFO':
return {
...state,
payload: [...state.payload, action.payload],
};
In my case:
I getting all fields through the react hook form and trying to update my state. I need to update the state without losing the whole data.
The code below works but deletes the rest of the data from the state.
case actionTypes.UPDATE_SETTINGS_INFO:
draft.payload = action.payload;
break;
I need to update half of the state at one time.
What is the best way to clone the state without losing the whole data?