Faz Fazzi
Faz Fazzi

Reputation: 13

cross feature selectors in new ngrx14+

i'm trying to use the new syntax createFeature() for my new angular project but there is this one thing stopping me, i don't know how to implement cross feature selectors using the new api i'm reading from this link: https://dev.to/this-is-angular/the-new-ngrx-lf1

in the "Extra selectors on feature" it explains how to add your own selectors, here the relevant code (see 'extraSelectors' prop)

export const Feature = createFeature({
    name: 'feature',
    reducer: createReducer(
        initialState,
        on(
            FeatureActions.loadFeature,
            (state) => ({
                ...state,
                loading: true,
            }),
        ),
        on(
            FeatureActions.loadFeatureSuccess,
            (state, { data }) => ({
                ...state,
                data,
                loading: false,
            }),
        ),
        on(
            FeatureActions.loadFeatureFailure,
            (state, { error }) => ({
                ...state,
                error,
                loading: false,
            }),
        ),
    ),
    extraSelectors: ({selectData}) => ({
        selectDataAsArray: createSelector(
            selectData,
            (data) => Object.values(data)
        ),
    }),
});

my doubts: what exactly is "selectData" there? And more importantly how can i load another feature selector if i want to create a new selector based on different features? i guess for cross selectors i still have to do the usual way, like:

export const selectCrossFeatureExampleSelector= createSelector(
  selectFeature1Something,
  selectFeature2Something,
  (something: Feature1Obj, somethingElse: Feature2Obj) => ({ something, somethingElse })
);

where selectFeature1Something and selectFeature2Something are defined in 'extraSelectors' of the related features?

Thanks

Upvotes: 1

Views: 85

Answers (0)

Related Questions