Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I Unsubscribe reactive form's valueChanges?

Tags:

Is it necessary to unsubscribe reactive form control's valueChanges subscription or Angular will unsubscribe it for us ?

In Router :
According to the official documentation, Angular should unsubscribe for you, but apparently, there is a bug.

So my concern here is the same for FormControl's valueChanges.

like image 492
jigs_ Avatar asked Jan 04 '18 14:01

jigs_


People also ask

Should you unsubscribe from form Valuechanges?

You must unsubscribe to prevent memory leaks and to avoid unexpected side-effects in your application.

What happens if we don't unsubscribe in Angular?

🎩 Automagically Unsubscribe in Angular As you probably know when you subscribe to an observable or event in JavaScript, you usually need to unsubscribe at a certain point to release memory in the system. Otherwise, you will have a memory leak. A memory leak occurs when a section of memory that is no longer being…

Do I need to unsubscribe from NgRx select?

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.

Do I need to unsubscribe from service Angular?

Since an Angular service never get's destroyed, unless your entire application get's destroyed, there is no real reason to unsubscribe from it. The observable will either complete or error or keep going as long as your application does.


1 Answers

Yes it is necessary, in order to not cause memory leaks with lost references to subscriptions you should always unsubscribe on ngOnDestroy. Or you could use other techniques to close the subscription when you no longer need it like takeUntil or takeWhile

See this post for more details about unsubscribing

http://brianflove.com/2016/12/11/anguar-2-unsubscribe-observables/

like image 98
tt9 Avatar answered Sep 21 '22 07:09

tt9