I am using ngrx/store in my Angular 5 project. The Application state I store looks something like this
class AppState{
private customerList: Customer [];
private selectedCustomer: Customer;
private countriesOperational: Country [];
}
I have individual reducers for each property of the state object, so I can listen to changes individually for each property. This state is then exposed via a service to the application components.
In my reducer (SelectedCustomerReducer), one of the actions is to replace the currently selected customer (2nd property above) with a new Customer object. I am confused about how the reducer should return the new value.
My reducer, already gets a new Customer object in the action.payload
; so should i just return that as the new state?
For e.g.
export function SelectedCustomerReducer(state: Customer = new Customer(), action: Action){
switch(action.type){
case 'updateSelectedCustomer':
return action.payload;
//OR
return Object.assign({}, state, action.payload);
}
}
SelectedCustomerReducer
. Rather just aCustomersReducer
where at the top level of it you have a variableselectedCustomer
– maxime1992CustomersReducer
should I return a new instance of AppState, whenever a property changes? For e.g. ifselectedCustomer
changes, do I return a new instance of AppState with the corresponding property updated? – Vikas