Reducers as State Management

Reducers are a simple idea that turns out to be very powerful. With Redux, youreplay a series of actions into the reducer and get your new application state as a result.

Reducers in a Redux application should not mutate the state, but _return a copy_of it, and be side-effect free. This encourages you to think of your applicationas UI that gets "computed" from a series of actions in time.

Simple Reducer

Let's take a look at a simple counter reducer.

app/store/counter/counter.reducer.ts

  1. import {Action} from '@ngrx/store';
  2. import {CounterActions} from './counter.actions';
  3. export default function counterReducer(state: number = 0, action: Action): number {
  4. switch (action.type) {
  5. case CounterActions.INCREMENT:
  6. return state + 1;
  7. case CounterActions.DECREMENT:
  8. return state - 1;
  9. case CounterActions.RESET:
  10. return 0;
  11. default:
  12. return state;
  13. }
  14. }

We can see here that we are passing in an initial state (the current number) andan Action. To handle each action, a common approach is to use a switch statement. Instead of each reducer needing to explicitly subscribe to the dispatcher, every action gets passed into each reducer, which handles the actions it's interested in and then returns the new state along to the next reducer.

Reducers should be side-effect free. This means that they should not modify things outside of their own scope. They should simply compute the next application state as a pure function of the reducer's arguments.

For this reason, side-effect causing operations, such as updating a record in a database, generating an id, etc. should be handled elsewhere in the application,like in your action creators or using @ngrx/effects.

Complex Reducer

Another consideration when creating your reducers is to ensure that they are immutable and not modifying the state of your application. If you mutate your application state, it can cause unexpected behavior. There are a few ways to help maintain immutability in your reducers. One way is by using new ES6 features such as Object.assign() or the spread operator for arrays.

app/models/counter.ts

  1. // ...
  2. export function setCounterCurrentValue(counter: Counter, currentValue: number): Counter {
  3. return Object.assign({}, counter, { currentValue });
  4. }
  5. // ...

Here, the setCounterCurrentValue() function creates a new Counter object that overwrites the counter.currentValue property with a new value while maintaining the references and values of all of the other properties from counter.

Let's update our reducer to utilize this concept:

  1. import {Action} from '@ngrx/store';
  2. import {Counter, createDefaultCounter, setCounterCurrentValue} from '../../models/counter';
  3. import {CounterActions} from './counter.actions';
  4. export function counterReducer(
  5. counter: Counter = { currentValue: 0 },
  6. action: Action
  7. ): Counter {
  8. switch (action.type) {
  9. case CounterActions.INCREMENT:
  10. return setCounterCurrentValue(counter, counter.currentValue + 1);
  11. case CounterActions.DECREMENT:
  12. return setCounterCurrentValue(counter, counter.currentValue - 1);
  13. case CounterActions.RESET:
  14. return setCounterCurrentValue(counter, 0);
  15. default:
  16. return counter;
  17. }
  18. }

With each action, we take the existing counter state and create a newstate with the updated value (such as counter.currentValue + 1).

When dealing with complex or deeply nested objects, it can be difficult to maintain immutability in your application using this syntax. This is where a library like Ramda can help.

原文: https://angular-2-training-book.rangle.io/handout/state-management/ngrx/reducers_as_state_management.html