Reputation: 11641
How to call selector inside selector in ngrx?
I have selectors from the adapter:
const { selectAll, selectEntities } = productAdapter.getSelectors();
And another selectors to get the products and then the product by id I pass in the props.
const getProductsEntities = createSlector(getProductState, (state) => selectEntites(state));
const selectProductById = createSelector(getProductsEntities , (entities, props) => {
return entities[props?.productId]
});
Now I want to do another selector and use the selectProductById
selector.
const selectProductViewById = createSelector(
selectProductById,
(product) => {
...
}
)
productView$ = this.store.pipe(select(selectProductViewById, { id: 1 });
But this is not working. Also I get error that the parameter passing is wrong.
So how to call selector inside selector with parameter?
Upvotes: 5
Views: 2260
Reputation: 306
First, you don't have to create a new getProductsEntities
selector by calling createSelector
. You can use selectEntities
directly in the select
call to store
. Or if you prefer another name you can simply do:
const getProductsEntities = selectEntities;
But to answer your question, here is how to implement a selector that takes a parameter as input using factory selectors:
const selectProductById = (productId: string) => createSelector(
getProductsEntities,
(entities) => entities[productId]
);
const selectProductViewById = (productId: string) => createSelector(
selectProductById(productId),
(product) => {
...
}
)
productView$ = this.store.select(selectProductViewById(1));
Upvotes: 3
Reputation: 15507
In the selector you're using props?.productId
but when you call the selector you use { id: 1 }
.
This should be the same property key.
Upvotes: 0