-2

I am working on the Spartacus project and I am using detectchanges() method to update the view in angular 10. The code reviewer commented that it causes leads to severe performance issue instead of that they suggesting to use observable.

What is the best practice?

James Z
  • 12,209
  • 10
  • 24
  • 44
Angular js
  • 51
  • 9

1 Answers1

4

Probably, your reviewer is right. First of all detectChanges() method could be used only in components with ChangeDetectionStrategy.OnPush if component rendering with Default Change Detection Strategy usage of detectChanges() not safe and will harm performance, for sure. Next important point is that you should not call detectChanges() without calling detach() method before detectChanges() - see official documentation. And last but not least is that in most cases is enough to call .markForChanges() method instead of detectChanges(). You have to understand that when you call detectChanges() you take responsibility of Change Detection algorithm on your self. Be careful with that stuff.

alexJS
  • 626
  • 6
  • 11