I need to Remove a state value if another state part has changed. How should I access to other store parts from a reducer in NgRx?
my store schema:
a: {...}
b: {...}
I need to access from a reducer with 'a' feature store to 'b' feature store which is initialized in another reducer.
You resolve that with @ngrx/effects.
An Effect spies the action stream and filters for an action and react on it.
Your filter in the effect for the action, which changes the related part and dispatch an other action which changes the other part of the store. (you only have to return the new action to the effect)
@Injectable()
export class MovieEffects {
loadMovies$ = createEffect(() => this.actions$.pipe(
ofType(ActionsOfA.ChangeAction), // which changes the related part
map(action=>ActionsOfB.RemoveOtherPart // dispatch
)
);
const aReducer = createReducer(initialState,
on((ActionsOfA.ChangeAction, {payload}), state => ({...state, a: payload}))
);
const bReducer = createReducer(initialState,
on(ActionsOfB.RemoveOtherPart, state => ({...state, b:{} )
);
In the filtered action there is also the payload, if you need it..