Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Angular (click) events triggers host component re-rendering

While trying to reduce useless component renders in an application I noticed Angular triggers changeDetection on click events, even for components with ChangeDetectionStrategy.OnPush

I’ve made a minimal example to reproduce the issue: stackblitz

Is there a way to limit renderings only on Input changes or async pipes updates ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

If you call function in template it will get called each time Change Detection is triggered.
This is considered very bad practice and you should avoid it at all cost.
(Do a quick google on the topic, there are a lot of resources explaning this more into details)

The proper way to check when the component is rerendered you should use lifecycle hooks for instance ngOnChanges.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading