DEV Community

Cover image for Angular signals and OnPush: a practical guide
Mohammad Khalifeh
Mohammad Khalifeh

Posted on Originally published at mkcodestudio.com on

Angular signals and OnPush: a practical guide

Angular signals change how a component tells the framework that something on screen is out of date. Combined with OnPush change detection, they let Angular skip almost every component on every tick and re-render only the ones whose data changed.

What a signal is

A signal is a value with a list of readers. When you call count() inside a template, Angular records that the template depends on count. When you call count.set(5), Angular knows exactly which views need to update.

readonly count = signal(0);
readonly doubled = computed(() => this.count() * 2);

increment() {
  this.count.update(n => n + 1);
}

Enter fullscreen mode Exit fullscreen mode

computed is lazy and cached. It recalculates only when one of the signals it read has changed, and only when someone reads it.

Why OnPush matters

With the default strategy, Angular checks every component after every event, timer and HTTP response. On a small page that is fine. On a page with a long list, a chart and a few forms, it adds up.

ChangeDetectionStrategy.OnPush tells Angular to check a component only when:

  • one of its inputs gets a new reference,
  • an event fires inside it,
  • or a signal it reads in the template changes.

The last point is what makes signals and OnPush a good pair. You stop thinking about references and markForCheck(), because reading a signal in the template is enough.

Patterns I use

Inputs as signals. input() and input.required() return signals, so derived values are plain computed calls instead of ngOnChanges bookkeeping.

readonly project = input.required<Project>();
readonly year = computed(() => new Date(this.project().date).getFullYear());

Enter fullscreen mode Exit fullscreen mode

HTTP results as signals. toSignal(this.http.get(...)) or the newer resource() API give you a value, a loading state and an error in one place. The template can show a skeleton while the value is undefined.

Effects for side effects only. effect() is for things outside Angular: writing to localStorage, setting the document title, logging. If you find yourself setting another signal inside an effect, a computed is almost always the better tool.

Things that catch people out

  • Reading a signal outside a reactive context (in a constructor, for example) gives you the current value once. It does not subscribe.
  • Mutating an object inside a signal does not notify anyone. Use update and return a new object or array.
  • computed must not have side effects. Angular may call it at any time, or not at all.

Where it pays off

On a portfolio or content site, the gain is less about raw speed and more about predictability. When every component is OnPush and every piece of state is a signal, you can look at a template and know exactly what makes it re-render. That makes performance problems easier to find and much rarer to begin with.


Originally published on mkcodestudio.com.

Top comments (0)