ngrx/store metareducers vs META_REDUCERS

I'm trying to figure out what is the difference between those two ways of providing metaReducers to NgModule: in imports array: StoreModule.forRoot(reducers, { metaReducers }) and second approach is to not include metaReducers in forRoot method but add it in providers array like: { provider: META_REDUCER, useFactoru: myCustomFuncToGetMetaReducers }.

What is the difference between first and second approach?

2 answers

  • answered 2019-09-15 10:21 ChrisY

    A little bit contrived but: A (lazy-loaded) module could inject MetaReducers via the injection token when it is loaded without having to create a StoreModule.forFeature in it.

    When could be this the case? If you just want to provide the MetaReducer and no other reducer logic or feature logic.

    Cheers Chris

  • answered 2019-09-15 16:51 timdeschryver

    The META_REDUCERS token is aimed for internal NgRx meta reducers or libraries. An example, the runtime checks provided by NgRx.

    A 3rd party library could use add a meta reducer, so you only have to import the module instead of adding the metareducer yourself to the metareducers option.