I am facing an issue where I need to update a property of a state object without creating a new object. Is there a way to add or update a single property without replacing the entire object?
Below is the reducer code:
const initialState = {
all: [],
single: {}
}
export const ModelsReducer: ActionReducer<any> = (state: IModelsStorage = initialState, action: Action) => {
switch(action.type) {
case GET_MODELS_SUCCESS: return Object.assign({}, state, {
all: [...action.payload.data]
});
case GET_MODEL_SUCCESS: return Object.assign({}, state, {
single: action.payload.data
});
// How do I only update the single object without overwriting the whole thing?
case GET_VARIANTS_SUCCESS: return Object.assign({}, state, {
single: {
variants: action.payload.data
}
});
default: return state;
}
}
Can someone guide me on how to achieve this?