createMutableDuck

Prerequisites

Please read about createMutableDuck first before continue here.

Mutable Action Dispatcher

Working with immutable operations can be hard. It can also lead to code that is more difficult to read. The library immerjs/immer allows you to simply mutate your state tree. Immer will take care about immutability for you.

NgRx Ducks provides a binding to immer that allows you to simply mutate your state. Therefore you simply need to use the function createMutableDuck. Its usage is nearly the same like createDuck. The only difference is that you do not need to return the next state in a reducer function since immer takes care of it.

import { createMutableDuck } from '@ngrx-ducks/core';

@StoreChunk()
export class CounterMutableFacade {
  // Basic usage without reducer
  loadCount = createMutableDuck('[Counter] Load Count', dispatch<number>());

  // Usage with a case reducer
  increment = createMutableDuck(
    '[Counter] Increment value',
    (state: CounterState, payload: number) => (state.count += payload)
  );
}

You will find a full example of a mutable facade at StackBlitz.

Last updated