As per my understanding this is how spread operator works:
x=[1,2,3];
y=[...x,4,5];
// this is same as y=[1,2,3,4,5]
const initialState={
ingredients: [
new Ingredient('Apples', 5),
new Ingredient('Tomatoes', 10),
]
};
export function shoppingListReducer( state=initialState, action:ShoppingListActions.ShoppingListActions ) {
switch(action.type) {
case ShoppingListActions.ADD_INGREDIENT:
return {
...state,
ingredients:[...state.ingredients,action.payload ]
}
default:
return state;
}
Here in the above example what does
return {
...state,
ingredients:[...state.ingredients,action.payload ]
}
evaluate to?
Payload is of type Ingredient :
export class Ingredient {
constructor(public name: string, public amount: number) {}
}