NGRX Entity State changes causes other slices to update

682 Views Asked by At

I have multiple EntitySates in the same slice of state. When I update with one of my entity adapters, the selector observable of the other EntityStates also gets triggered, although the selector is not accessing the updated slice of state. Is this behaviour expected?

State:

export interface ComputerState extends EntityState<Computer> {
}

export interface PersonState extends EntityState<Person> {
}

export interface DataState {
    computer: ComputerState;
    person: PersonState;
}

The following reducer action causes the person selector observable to emit a new value:

on(Actions.updateComputerExample, (state, { id }) => {
        return {
            ...state,
            computer: computerAdapter.updateOne(
                { id: id, changes: { name: 'test' } },
                state.computer
            ),
        };
    }),

Person Selector:

export const selectPersons= createSelector(
    selectState,
    state=> state? personAdapter.getSelectors().selectAll(state.person) : null
)

Does someone have a solution to this?

1

There are 1 best solutions below

4
On

You should be coding your reducer like this:

on(Actions.updateComputerExample, (state, { id }) => {
   return computerAdapter.updateOne(
      { id: id, changes: { name: 'test' } },
      { ...state });
}),