praand
praand

Reputation: 1

NGRX Entity State changes causes other slices to update

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?

Upvotes: 0

Views: 741

Answers (1)

Michael Kang
Michael Kang

Reputation: 52837

You should be coding your reducer like this:

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

Upvotes: 1

Related Questions