rhbc73
rhbc73

Reputation: 767

fetch store data in ngrx selector

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

Upvotes: 0

Views: 452

Answers (1)

timdeschryver
timdeschryver

Reputation: 15507

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)

Upvotes: 0

Related Questions