createMutableDuck

Prerequisites

circle-exclamation

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/immerarrow-up-right 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)
  );
}
circle-info

You will find a full example of a mutable facade at StackBlitzarrow-up-right.

Last updated

Was this helpful?