Currently, I have an app with a ngrx store set up. I am experiencing an issue where, upon clicking a button, the function that fetches data from the store returns multiple copies of the data. Upon subsequent clicks, the number of returned copies grows exponentially.
In my component.ts
, the selector connected to the store used to fetch the data looks like this:
this.data$ = this.store.pipe(select(selectors.dataSelector));
The function triggered on click event in my HTML is as follows:
onClick() {
this.data$.subscribe(x =>
console.log(x)
);
}
After one iteration:
https://i.stack.imgur.com/9dARp.png
After two:
https://i.stack.imgur.com/yxLRm.png
After three:
https://i.stack.imgur.com/iLVeZ.png
This pattern continues with each click. I am concerned about the impact on performance and wonder why this is occurring. Is there an alternative method to obtain data from the store in the component.ts
while ensuring that the data is only retrieved once?