How can I use withLatestFrom with a parametrized selector?

Discussão em 'Angular' iniciado por Natalia Cáceres, Janeiro 31, 2025.

  1. I've got a parametrized selector like this

    mySelector = (myId: number) => createSelector(selectList, list => list.find(item => item.id = myId);


    and I need to use it in an effect, like this

    myEffect$ = createEffect(
    () => {
    this.actions$.pipe(
    ofType(myAction),
    withLatestFrom(this.store.select(mySelector(myAction.ItemId)),
    // this doesn't work bc action is not declared in this scope
    map(([{itemId}, myItem]) => {
    // I need these two values in the operators downstream
    (...)
    }),
    (...)
    )
    }
    )


    This is what I am doing now bc I haven't been able to find a better way around this very common scenario.

    myEffect$ = createEffect(
    () => {
    this.actions$.pipe(
    ofType(myAction),
    mergeMap(({itemId}) => this.store.select(mySelector(itemId))
    .pipe(
    map(myItem => [myItem, itemId])
    )
    ),
    map(([myItem, itemId]) => {
    (...)
    }),
    (...)
    )
    }
    )


    is there any better, faster, more performant way to achieve the same results?

    Continue reading...

Compartilhe esta Página