Angular comes with two component change detection strategies: default
and onPush
.
Default
is used by default: Angular will check if your component needs to be refreshed every time something happens in the browser. Zone.js triggers such change detection by notifying Angular when a DOM event happens (someone clicked on a button) or a setTimeout
completes, or an HTTP request completes.
In other words, any time a callback function runs in the browser, Angular will check if our components need to be re-rendered with new data.
With onPush
, the change detection behavior changes. onPush
indicates that our component only relies on inputs to display data (in other words – it’s a presentation component) and that DOM events or HTTP requests do not impact the HTML rendering of that component.
As a result, you can use onPush
to improve the performance of your presentation components, which is another good reason to follow the presentation vs. container components approach covered yesterday. The official documentation here shows an in-depth dive into change detection strategies.