Search code examples
angularrxjsngrxngrx-store

createSelector - I'm doing it wrong


I thought this was right, but it doesn't work:

  selectClaimTypeById: ((any) => MemoizedSelector<any, ClaimType[]>) = (props: any) => createSelector (
    this.claimtypeSelector,
    (claimTypes: ClaimType[]) => claimTypes
      .filter ((claimType: ClaimType) => (claimType.claimType === props.claimType)
      ))

select(this.claimtypeSelector) correctly returns an array of ClaimTypes. I THOUGHT the code above when called as select(selectClaimTypeById, {claimId: id}), would return a filtered array of one claimType.

It returns a memoized function

this.store
  .pipe (
    select (this.selectors.selectClaimTypeById, {claimType: id}),
    take (1))
  .subscribe (data => record = data);

record = function memoized

I'm obviously missed something

Any help?


Solution

  • The selector is defined as a function, so you have to invoke it:

    this.store
      .pipe (
        select (this.selectors.selectClaimTypeById(id)),
        take (1))
      .subscribe (data => record = data);
    

    For more examples and an explanation see Parameterized selectors