简体   繁体   中英

ngrx effects called multiple times after it return the value

ngrx effects called multiple times after it return the value.

loadMovies$: Observable<Action> = createEffect(() => {
    return this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
        .pipe(
          map(movies => {
             return new counterActions.IncrementCounter();
          }));
      }
    ));
  });

You should add dispatch: false to your effect

  loadMovies$ = createEffect(() => {
    this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
        .pipe(
          map(movies => {
             return new counterActions.IncrementCounter();
          }));
      }
    )),
    { dispatch: false };
  });

Example from document

  logActions$ = createEffect(() =>
    this.actions$.pipe(
      tap(action => console.log(action))
    ), { dispatch: false });

👋 it's not the first time I've seen this issue pop up, as it's an easy-to-make-and-overlook one.

That's why I added a rule for it in ngrx-tslint-rules to prevent it in the future.

Because of createEffect function. You should remove wrapper createEffect function and just use this.actions$.pipe, instead. That will fix your issue. I already did and now it works okay.

loadMovies$ = this.actions$.pipe(
    ofType(counterActions.CounterActionTypes.IncrementCounter),
    flatMap(() => {
        return this.userService.counter()
          .pipe(
              map(movies => {
                  return new counterActions.IncrementCounter();
              })
          );
    })
);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM