I need to build an Effect and I need a value from the store, the problem is that the selector is a selector with parameters.
following the example code:
@Effect()
ExampleEffect$ = this.actions$.pipe(
ofType(
ActionTypes.SOMETHING
),
map((action: Somthing) => action.payload.myParameter),
// HERE I NEED THE PARAMETER TO PERFROM THE SELECTION
withLatestFrom(this.store.pipe(select(selectorWithParamter(myParameter))),
map((value) => /* do somthing with the array [myParameter, valueSelected from sotre]*/)
Create Menu Selectors Similar to what you did with Actions, let's update the application to get data required by the components using NgRx Selectors. You can use Selectors by injecting NgRx's Store and calling it's select function passing in the selector's name, which returns the slice of the state that we need.
You can write an arrow function to pass parameters while creating a selector.
export const getAlbumById = (collectionId: number) => createSelector(getAlbumEntities, entities => entities[collectionId]);
More example
// effect
@Effect({ dispatch: true })
upsertAlbum$ = this.actions$.pipe(
ofType(AlbumActionTypes.UpsertAlbum),
map((action: any) => action.payload),
mergeMap(({ album }) =>
this.store.pipe(
select(selectAlbumIfExists(album.id)),
first(),
map(isAlbumHasName => [album, isAlbumHasName])
)
),
filter(([album, isAlbumHasName]) => !isAlbumHasName),
map(([album]) => new LoadAlbum({ album }))
);
// selector
export const selectAlbumIfExists = (id: string) =>
createSelector(
selectAlbumsEntities,
entities => !!(entities[id] && entities[id].name)
);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With