0

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?

praand
  • 1
  • 1

1 Answers1

1

You should be coding your reducer like this:

on(Actions.updateComputerExample, (state, { id }) => {
   return computerAdapter.updateOne(
      { id: id, changes: { name: 'test' } },
      { ...state });
}),
Michael Kang
  • 52,003
  • 16
  • 103
  • 135
  • this does not work, because the second parameter of updateOne only accepts the computer enity state. ...state would be the whole state. – praand Oct 29 '20 at 13:57
  • if it is a reducer for computer, then it should never be the whole state. – Michael Kang Oct 29 '20 at 17:02
  • I have multiple entity states in the same state -> the reducer is, in the case shown in the question, for computer and person – praand Oct 30 '20 at 09:15
  • Kind of similar to: https://medium.com/capital-one-tech/multiple-entities-in-a-single-ngrx-8-state-ed5fd082c3f0 , but like I said in the questions I have problems with the selectors. – praand Oct 30 '20 at 09:18