In @ngrx/store 2.0 we could provide the root reducer as a function and from there we split our logic inside the application. After I updated to @ngrx/store 4.0 I cannot use this feature any more from what I can see the reducers need to be a map of reducers which will create objects under the same keys in the state. Is there a way to use the old behavoir in @ngrx/store 4.0 In my state components are aware one of another and I need to be able to split my state dynamically also I need to be able to dispatch actions to the right reducer in my own way. Also app is splitted in multiple lazy loaded routes which in some cases reuse the data from another feature.
StoreModule.provideStore(reducer, {
auth: {
loggedIn: true
}
})
StoreModule.forRoot(reducers, {
initialState: {
auth: {
loggedIn: true
}
}
})
I need reducers
to be a function which gets the full state and dispatches it to the correct reducer, Is there a way to achieve this behavior?
You can set up a meta reducer to receive every event and manipulate the state from its root. Here is an example way to set it up:
The
StoreModule
forRoot()
function accepts areducerFactory
which can be used as follows:After I had a second look over ngrx repo I figured it out. To achieve the wanted result we need to replace the @ngrx/store reducer factory with a new implementation. I injected a new reducer factory and right now the application works as before. Simple code sample on how to replace the reducer factory it.
This works for me: