Search code examples
angularngrxngrx-storengrx-entity

Can I use single EntityAdapter for a few instance of the same EntityState?


I have a one EntityStates of the same model assigned to a few properties in the single state. Can I manipulate all of them using a single adapter? They have the same initial state but will contain values with various status.

interface myState {
   entity1: EntityState,
   entity2: EntityState
   entity3: EntityState
}

export interface EntityState extends EntityState<MyModel> {}

export const adapter: EntityAdapter<MyModel> = createEntityAdapter<MyModel>();

export const myInitialState: EntityState = adapter.getInitialState({
  loaded: false
});

export const initialState = {
   entity1: myInitialState,
   entity2: myInitialState
   entity3: myInitialState 
}

and then I would like to is with a single entity instance like that

state{
...state
entity1: adapter.addMany(payload {
  ...state.entity1
  loaded: true
 )
}

Solution

  • Yes, this will work. The adapter's function is pure, it takes some state and a "payload" and will returns a new updated state.

    The snippet you posted is valid.