Platform: How to register single reducer using StoreModule.forFeature with ngrx 8.0.0 beta

Created on 4 Jun 2019  路  9Comments  路  Source: ngrx/platform

Hi,

in my feature module, im using the createReducer method to create my reducer as follow:

export const reducer = createReducer(
new InitState(),
on(loadData, state => {
return {
...state,
isLoading: true
};
})
);

but when i register the above reducer in the module class as following:

StoreModule.forFeature('my-module', reducer),

im getting the following error when compiling the project:

ERROR myModule.module.ts(38,47): Error during template compile of 'MyModuleModule'
Function calls are not supported in decorators but 'createReducer' was called in 'reducer'
'reducer' calls 'createReducer' at myreducer.reducer.ts(14,24).

Do you know what might go wrong? or how do i register single reducer, because all the examples i have seen are using combineReducers with multiple reducers, and i wonder if for just one reducer, how do we do it?

Thank you much!

Most helpful comment

You have two options:

  • wrap with function
export const myReducer = createReducer(...);

export function reducer(state: State | undefined, action: Action) {
  return myReducer(state, action);
}

All 9 comments

You have two options:

  • wrap with function
export const myReducer = createReducer(...);

export function reducer(state: State | undefined, action: Action) {
  return myReducer(state, action);
}

@alex-okrushko thanks second option works! and Is wrapping the single reducer in a function a long term solution, or will createReducer() just work in the production version of ngrx 8.x? and when do we expect the production version of ngrx 8.x to be released?

and Is wrapping the single reducer in a function a long term solution, or will createReducer() just work in the production version of ngrx 8.x?

No sadly not, this is how the AOT compiler works.

when do we expect the production version of ngrx 8.x to be released?

We're laying the last stones for the ngrx 8 release.
Besides that we've hit a problem with prod builds https://github.com/ngrx/platform/issues/1905

@timdeschryver, ok thank you for the update! so far i really like the ngrx library

Wrapping the createReducer in a function is not working for me.

This is how I used it.

export function productsReducer(state, action) {
  return createReducer(
    initialState,
    on(ProductActions.AddProduct, (state, { product }) => adapter.addOne(product, state)),
    on(ProductActions.UpsertProduct, (state, { product }) => adapter.upsertOne(product, state)),
  );
}

It gives me an error entiry map can't be executed on undefined.

@varghesep updated the comment.

So in your example it would be

export function productsReducer(state, action) {
  return createReducer(
    initialState,
    on(ProductActions.AddProduct, (state, { product }) => adapter.addOne(product, state)),
    on(ProductActions.UpsertProduct, (state, { product }) => adapter.upsertOne(product, state)),
  )(state, action);
}

@alex-okrushko
I have reducer folder which contain multiple reducer file
image

each fle contain code in this way
image

now my requirement is, I want to merge all the reducers without creating multiple instances, must be only a single name that contains all reducers

@amirSohel007
you are probably looking for https://ngrx.io/api/store/combineReducers

@alex-okrushko this above approach will not work

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hccampos picture hccampos  路  3Comments

brandonroberts picture brandonroberts  路  3Comments

mappedinn picture mappedinn  路  3Comments

shyamal890 picture shyamal890  路  3Comments

Matmo10 picture Matmo10  路  3Comments