Angular signals and OnPush: a practical guide

Angular2 min read

JavaScript code with syntax highlighting on a monitor
Photo: Gabriel HeinzerUnsplash

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);
}

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());

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.

Have a platform in mind?

Tell me who will use it and what they need to do. I'll reply with questions and next steps.

Join the conversation

No comments yet. Be the first to share your thoughts.

Leave a comment

Questions, ideas or a different point of view are all welcome. Your comment will appear here once it has been approved.

Your email is never shown publicly. It is only used if you ask to be told about a reply.