xxxxxxxxxx
const getTotalItems = (items: CartItemType[]) =>
items.reduce((ack: number, item) => ack + item.amount, 0);
xxxxxxxxxx
const actionType={
INCREMENT:'INCREMENT',
DECREMENT:'DECREMENT'
};//define const that can be used in whole particular file without mistake
function reducer(state, action) {
switch (action.type) {
case actionType.INCREMENT: // defined at top
return {count: state.count + 1};
case actionType.DECREMENT: // defined at top
return {count: state.count - 1};
default:
return state;
}
}
dispatch({type: actionType.INCREMENT});
<button onClick={() => dispatch({type: actionType.INCREMENT})}>Increment</button> // defined at top
<button onClick={() => dispatch({type: actionType.DECREMENT})}>Increment</button> // defined at top
xxxxxxxxxx
const input = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const sumOfConsecutives = (value1, value2) => value1 + value2;
const sumOfNums = input.reduce(sumOfConsecutives);
/* result:
{
input: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
sumOfNums: 55,
}
*/