I have a very simple state in my store:
const state = {
records: [1,2,3],
};
I have a selector for records:
export const getRecords = createSelector(getState, (state: State) => state.records));
And what I want now is to have separate selectors for fetching each record by index. For this purpose I want to create one generic selector with props in this way:
export const getRecordByIndex = createSelector(
getRecords,
(state: State, { index }) => state.records[index]),
);
And after that create a couple of specific selectors e. g.:
export const getFirstRecord = createSelector(
getRecordByIndex(/* somehow pass index = 0 to this selector */),
(firstRecord) => firstRecord),
);
But I didn't find any mention how to pass parameters to selectors with props when we use them inside createSelector method. Is it possible?
With fixed parameters for the selector it works fine:
but what's about dynamic parameters:
that did not work in my app :-( (NgRx version 8)
I am using
"@ngrx/entity": "7.2.0",
and I can see that props are passed to each selector, for example in my component I am calling:And then in my reducer I have the following:
From this blog post: https://medium.com/angular-in-depth/ngrx-parameterized-selector-e3f610529f8
Ah ... but rereading your question, you are asking then how to build another selector that uses this selector? The above-linked article suggests building a factory function.
You could use the projector function: