Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unsubscribe from ngrx/store?

Tags:

I have a component which gets its data from subscribing to a store.

this.store.select('somedata').subscribe((state: any) => {   this.somedata = state.data; }); 

I want to unsubscribe from this subscription when component is no more, in other places where I am subscribing to some observable, something like this:

this.service.data.subscribe(    (result: any) => {//data} ); 

I unsubscribed it on ngOnOnDestroy, like this:

ngOnDestroy(){    this.service.data.unsubscribe(); } 

But in case of store I'm not able to, it gives me error:

Property 'unsubscribe' does not exist on type 'Store<State>' 
like image 284
Michael Philips Avatar asked Nov 26 '17 12:11

Michael Philips


People also ask

Do you need to unsubscribe from NgRx store?

Effects are the primary way of handling logic within NgRx. They allow us to observe streams of actions and react to them. We do not need to subscribe to any effect nor do you need to manually unsubscribe. That is handled internally by the NgRx Effects module; once again taking that burden out of our hands.

How do I unsubscribe?

On your Android device, go to your subscriptions in Google Play. Select the subscription you want to cancel. Tap Cancel subscription. Follow the instructions.

How do I unsubscribe from RxJS observable?

Unsubscribing Manually One method we can use, is to unsubscribe manually from active subscriptions when we no longer require them. RxJS provides us with a convenient method to do this. It lives on the Subscription object and is simply called . unsubscribe() .

Where can I find documentation for the current version of ngrx?

Please visit ngrx.io to see documentation for the current version of NgRx. Store is RxJS powered state management for Angular applications, inspired by Redux. Store is a controlled state container designed to help write performant, consistent applications on top of Angular.

Do I need to call complete and unsubscribe after ngdestroyed$next?

Then you don't have any need for calling complete and unsubscribe. One must call ngDestroyed$.complete () after ngDestroyed$.next (), otherwise you will leak the subject. It might be a small object but it will remind active... About order: takeUntil is always last, with the expectation of shareReply, multicast and similar multicasting operators.

Is there a way to unsubscribe from a store type?

Property 'unsubscribe' does not exist on type 'Store<State>' There's a better way than the top voted answer, a way in which you don't have to manage a bunch of subscriptions, only one. Then you can have as many subscriptions as you want without having to create a bunch of unnecessary vars.

How do I unsubscribe from a subscription in MongoDB?

When you subscribe you will receive a subscription object on it you can call unsubscribe () const subscription = this.store.select ('somedata').subscribe ( (state: any) => { this.somedata = state.data; }); // later subscription.unsubscribe ();


2 Answers

There's a better way than the top voted answer, a way in which you don't have to manage a bunch of subscriptions, only one. Then you can have as many subscriptions as you want without having to create a bunch of unnecessary vars.

  public ngDestroyed$ = new Subject();    public ngOnDestroy() {     this.ngDestroyed$.next();   }    public ngOnInit() {     this.myWhateverObs$         .pipe(takeUntil(this.ngDestroyed$))         .subscribe((val)=> { this.whatever()});     this.myWhateverNPlusOne$         .pipe(takeUntil(this.ngDestroyed$))         .subscribe((val)=> { this.whatever()})   } 
like image 192
FlavorScape Avatar answered Sep 28 '22 07:09

FlavorScape


When you subscribe you will receive a subscription object on it you can call unsubscribe()

const subscription = this.store.select('somedata').subscribe((state: any) => {   this.somedata = state.data; }); // later subscription.unsubscribe(); 

or

ngOnInit(){  this.someDataSubscription = this.store.select('somedata').subscribe((state: any) => {   this.somedata = state.data;  }); }  ngOnDestroy(){   this.someDataSubscription.unsubscribe(); } 
like image 25
G.Vitelli Avatar answered Sep 28 '22 08:09

G.Vitelli