|
1 | | -# Basic Reducers |
| 1 | +# Basic Reducers |
| 2 | + |
| 3 | +Reducers are the foundation of any @ngrx/store application. Each reducer manages a slice, or section of state, and can be thought |
| 4 | +of almost like a table in a database (store). Let's start with the basics to fully understand what a reducer is and the role they serve. |
| 5 | +First, let's familiarize ourselves with the reduce function in JavaScript. |
| 6 | + |
| 7 | +### Reduce |
| 8 | +*arr.reduce(callback[, initialValue])* |
| 9 | + |
| 10 | +The reduce function accepts an `accumulator` and the `current` value in the array, returning a new value which becomes the `accumulator` on the next iteration. |
| 11 | +A starting value can also be supplied as the second parameter. |
| 12 | + |
| 13 | +```ts |
| 14 | +//add values in an array |
| 15 | +const example = [1,2,3].reduce((accumulator, current) => accumulator + current); |
| 16 | + |
| 17 | +console.log(example); //6 |
| 18 | +``` |
| 19 | + |
| 20 | +Let's extract our anonymous reducer function out and name it `sum`, for the same result. |
| 21 | + |
| 22 | +```ts |
| 23 | +//named reducer function |
| 24 | +const sum = (accumulator, current) => accumulator + current; |
| 25 | +const example = [1,2,3].reduce(sum); |
| 26 | + |
| 27 | +console.log(exampleTwo); //6 |
| 28 | +``` |
| 29 | + |
| 30 | +In @ngrx/store we want state changes to be caused by dispatched actions. Each action has a type and an optional payload. |
| 31 | +Let's rename the accumulator parameter to `state` and adjust the `current` parameter to accept an `action` type. Each action that passes through the 'reducer' function will return a new representation of state. |
| 32 | + |
| 33 | +```ts |
| 34 | +const sumReducer = (state, action) => { |
| 35 | + switch(action.type){ |
| 36 | + case 'ADD': |
| 37 | + return state + action.payload; |
| 38 | + default: |
| 39 | + return state; |
| 40 | + } |
| 41 | +}; |
| 42 | +/* |
| 43 | + Each action has a type and an optional payload |
| 44 | +*/ |
| 45 | +const actions = [{type: 'ADD', payload: 1},{type: 'ADD', payload: 2},{type: 'ADD', payload: 3}]; |
| 46 | +/* |
| 47 | + We can now reduce our array of actions into the same result as above. |
| 48 | +*/ |
| 49 | +const exampleThree = actions.reduce(sumReducer, 0); |
| 50 | + |
| 51 | +console.log(exampleThree); //6 |
| 52 | +``` |
| 53 | + |
| 54 | +To further solidify this concept, we can also create a counter reducer that increments or decrements a value based on an action type. |
| 55 | + |
| 56 | +```ts |
| 57 | +const counterReducer = (state, action) => { |
| 58 | + switch(action.type){ |
| 59 | + case 'INCREMENT': |
| 60 | + return state + 1; |
| 61 | + case 'DECREMENT': |
| 62 | + return state - 1; |
| 63 | + default: |
| 64 | + return state; |
| 65 | + } |
| 66 | +}; |
| 67 | +//sample actions |
| 68 | +const moreActions = [{type:'INCREMENT'}, {type:'INCREMENT'}, {type:'DECREMENT'}, {type:'INCREMENT'}]; |
| 69 | +const exampleFour = moreActions.reduce(counterReducer, 0); |
| 70 | + |
| 71 | +console.log(exampleFour); //2 |
| 72 | +``` |
| 73 | + |
| 74 | +*Discuss scan...in progress* |
0 commit comments