fetch store data in ngrx selector

432 Views Asked by At

say in my store I have a feature with an array of data and an index

{
    data: Data[],
    activeIndex: -1
}

What I want to do is, every time activeIndex changes, I get notified and get data[activeIndex]

so I have a selector

const selectActiveIndex = creatSelector(
    selectFeature,
    (feature) => feature.activeIndex
)

But how do I get data[activeIndex] in my component?

Thanks

1

There are 1 best solutions below

1
timdeschryver On

You can compose a selector based on other selectors, here you can combine the results into a single result. This combined selector can then be used in the component.

const selectActiveIndex = creatSelector(
    selectFeature,
    (feature) => feature.activeIndex
)

const selectData = creatSelector(
    selectFeature,
    (feature) => feature.data
)

const selectActiveData = createSelector(
  selectActiveIndex,
  selectData,
  (idx, data) => data[idx]
) 

component:

activeData$ = this.store.select(selectActiveData)

constructor(private store:Store)