I have a pretty simple app, and am using ngrx for the first time
reducer.module.ts
@NgModule({
imports:[
StoreModule.forRoot({
sliceA: AReducer,
sliceB: BReducer
})
]
})
AReducer.ts
export function AReducer(state, action : AActions) {
switch(...)
}
BReducer.ts
export function AReducer(state, action : BActions) {
switch(...)
}
someComponent.ts
store.dispatch(new AAction());
Now what I would have hoped is ngrx would only trigger AReducer and not BReducer, but I see it triggering both. It's also jarring because the second argument to BReducer is an AAction, even though I parameterize the second argument to the type BAction.
So is there any way I can have ngrx only trigger the relevant reducers? Or is the design that it triggers all of them, and the irrelevant ones just trivially return the store?
To my understanding, this is the typical NgRx
behaviour. For NgRx
, all actions
are independent/unrelated regardless of which file you define them in. Sames goes for reducers
, you'll use different files to make it maintainable, but NgRx
goes through all the reducers for each action dispatched.
However, what you'd typically do in a reducer
is that, when a case
to handle an action is implemented, if you return
from that case
, NgRx
stops looking for the rest of the reducers.
So in your case:
export function AReducer(state, action : AActions) {
switch(action.type){
case actions.AAction:
return {...state} // this is when NgRx stops looking for the case to handle an action
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With