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!
You have two options:
InjectionToken: https://github.com/ngrx/platform/blob/d81ddeb3938cb5768cf76a815c956a78de4df057/projects/example-app/src/app/reducers/index.ts#L37-L44export 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

each fle contain code in this way

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
Most helpful comment
You have two options:
InjectionToken: https://github.com/ngrx/platform/blob/d81ddeb3938cb5768cf76a815c956a78de4df057/projects/example-app/src/app/reducers/index.ts#L37-L44