Changing states with pure functions

In the previous section, we introduced the concept of the action and how that was the mediator through which we were allowed to change our state. We didn't change the state, though, in the normal sense of the word, but rather took the old state, applied the action, and produced a new state. To accomplish this, we need to use a pure function. In the context of Redux those are called reducers. Let's write ourselves a reducer:

// principles/first-reducer.jsmodule.exports = function reducer(state = {}, action) {  switch(action.type) {    case "SELECT_JEDI":      return Object.assign({}, action.payload);    default:      return state;  }}

We highlight the pure aspect of the preceding reducer. It takes our selectedJedi from the  ...

Get Architecting Angular Applications with Redux, RxJS, and NgRx now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.