ngrx-actions icon indicating copy to clipboard operation
ngrx-actions copied to clipboard

@Action - samples in the README and articles are incorrect?

Open lanovoy opened this issue 7 years ago • 0 comments

README and article samples are looking like we can mutate state object directly and don't have to return anything from the reducer function. It doesn't seem to work... If I do something like:

@Action(LoadAction)
  public load(state: IState, action: LoadAction) {
      state.isLoading: true,
      state.isLoaded: false,
      state.hasError: false,
      state.error: null
  }

It wouldn't work throwing exception about read-only variable modifications prohibited in the strict mode.

But if I change it to normal pure-function reducer, it will work just fine:

@Action(LoadAction)
  public load(state: IState, action: LoadAction) {
    return Object.assign({}, state, {
      isLoading: true,
      isLoaded: false,
      hasError: false,
      error: null
    });
  }

Tbh, the original idea of reducers as pure-functions is great... Sample code in the README looks like if it's a side-effect function with state mutation... if that's the case why then separate @Effect from @Action ? They are essentially the same...

lanovoy avatar Mar 06 '18 10:03 lanovoy