NGRX Entity - how to handle isSelected property properly?

59 Views Asked by At

I have a table where each row might be clicked and then it should change a color and later on be used to filter out selected items. In each row I have an item:

interface Item{
   id:string;
   name:string;
   price:number;
}

I am wondering what is a better approach regarding EntityStateAdapter:

  1. add isSelected property to Item and later on updating this part of state?

OR

  1. extend Adapter state and store selected items in array.
export const adapter: EntityAdapter<Item> = createEntityAdapter<Item>();

export const initialState: EntityState<Item> = adapter.getInitialState({
        selectedItems:Array<string>
})

IMO 1st approach is better. It is easier to just add property to Item, because later on I can easily reflect the state in table for each row just by item.isSelected. I would like to hear about your experience.

1

There are 1 best solutions below

0
timdeschryver On

I prefer to use selectedItems, reasons:

  • it's "meta data" that is added on top of an entity
  • easier to update, e.g. if you select or unselect you can simply update the array instead of looking the entity up before you can update it