Search code examples
angularngrx

Select object from store by id from store NgRx


How can i select an Building from my state depending on the selectedBuildingId property of my state?

my store:

export interface State extends EntityState<Building> {
    selectedBuildingId: string
}

Is it possible to have some selector similar to this one but rather than passing it the id, it just takes the one in the store?

export const selectById = (id: string) 
    => createSelector(selectEntities, (buildingsDic) => buildingsDic[id]);

My solution was to just fetch the id from the store and then fetch the Building with the id, but I feel like this is not good solution.


Solution

  • Lets say you have a selector for the selectedBuildingId called selectBuildingId, you can use a selector inside an other selector.

    export const selectById = createSelector(selectEntities, selectBuildingId, (buildingsDic, id) => buildingsDic[id]);
    

    Take a look at the NGRX doc