Search code examples
angulartypescriptreduxngredux

NgRedux + Angular + select triggered on subscribe


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


Solution

  • 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.