Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

430
Visualizações
ngrx 8: dispatch multiple actions in single effect

I need to dispatch multiple actions after calling an API request in my effect. I'm using this code at the moment to dispatch one action after API request done:

      changeStatus$ = createEffect(() => 
      this.actions$.pipe(
        ofType(fromJtDetail.changeStatus),
        switchMap(action =>
          this.jtDetailService.changeStatus(action.entity,action.jobTicketId).pipe(
          map(res => fromJtDetail.statusChanged({changedStatus: action.entity.newStatus})),
          catchError(error => EMPTY)
      ))));

it's important to dispatch more actions in this effect, can't write another effect for this.

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

All of the answers here are correct, the "simple" answer and solution is to return an array of actions. However, this is a bad-practice, for more info see No Multiple Actions In Effects from the docs of NgRx ESLint.

over 4 years ago · Santiago Trujillo Relatório

0

You can dispatch multiple actions using switchMap + of(

 changeStatus$ = createEffect(() => 
  this.actions$.pipe(
    ofType(fromJtDetail.changeStatus),
    switchMap(action =>
      this.jtDetailService.changeStatus(action.entity,action.jobTicketId).pipe(
      switchMap(res => of(
        fromJtDetail.statusChanged({changedStatus: action.entity.newStatus}),
        fromThere.doSmthAction(),    // <-- additional action 1
        formHere.doSmthElseAction(), // <-- additional action 2
      )),
      catchError(error => EMPTY)
  ))));

EDIT:
Althought it can be done you should not do it.
Have a look at no-multiple-actions-in-effects

over 4 years ago · Santiago Trujillo Relatório

0

You can pass the array of actions into a switchMap like below:

switchMap(result => ([new Action1(result), new Action2(result)])
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda