Search code examples
angulartypescriptngxs

How to create parametrized selector in NGXS?


I would like to create a selector with arguments. Is there a clean way to do it?


Solution

  • In the documentation, under selectors, you can find memoized selectors with arguments:

    @State<string[]>({
      name: 'animals',
      defaults: []
    })
    export class YourState{
    
      static selectorName(yourArgument: string) {
        return createSelector([YourState], (state: string[]) => {
          return state.filter(s => s === yourArgument);
        });
      }
    }
    

    You can then use your selector like this:

    @Select(YourState.selectorName('yourArgument')) selectedStuf;
    

    Next time, add some code examples from what you have already tried.