1

I'm using redux in my angular App for weeks now, but I just noticed something about it :

When I'm selecting a part of my store to monitor changes, let's say like that :

In the ngOnInit() of app.component.ts

this.ngRedux.select(s => s.counter).subscribe((counter: number) => {
    console.log(counter)
});

The console.log(counter) is executed each time my counter value change, that's ok, that's how it suppose to works.

BUT, the callback is also executed when I first subscribe. So it's like that :

  1. Enter onNgInit()
  2. Executing select()
  3. Executing Subscribe()
  4. Executing my callback
  5. ------- Later when counter change : -------
  6. Executing my callback

My problem is that I dont want my callback to be executed on step 4, because the store did not change !

Is there an issue in my understanding ? Is is supposed to work like that ? If yes : Can I do something to change it ?

Thanks

Nicolas Pretot
  • 182
  • 2
  • 12

1 Answers1

2

Unfortunately, this is the standard behaviour of .subscribe(). The only thing you can do is to compare your current value of counter with the new one and only react on it if changes happened.

  • Thanks, for your answer, I was wondering if it's the standard behavior on .subscribe() or if I did something wrong. I will try to solve this by my slef as you suggested. Thanks – Nicolas Pretot Jun 29 '18 at 07:23
  • Thanks for the clarification. Any idea where is it mentioned in the docs? – refaelio Sep 26 '19 at 15:57