Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is an ngrx action supposed to trigger all the reducers

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?

like image 229
sherz1 Avatar asked Sep 13 '25 17:09

sherz1


1 Answers

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
  }
}
like image 147
Aragorn Avatar answered Sep 16 '25 10:09

Aragorn