← Back to Angular, page 7

angular-0122 · Medium mid

What are afterNextRender and afterEveryRender, what are the phases, and when should you use them instead of lifecycle hooks?

afterNextRender runs a callback once after the next time Angular has finished rendering, and afterEveryRender runs after every render. They run only in the browser, never during server rendering, so they are the safe place for DOM measurements and third party libraries. Phases (earlyRead, write, mixedReadWrite, read) order reads and writes to avoid layout thrashing.

Full answer

Some work needs the real DOM: measuring an element, focusing an input, starting a chart library, or reading window. During server rendering there is no browser DOM, and in the browser the DOM is only correct after Angular has finished updating it.

afterNextRender(callback, options?) registers a callback that runs once, after the next completed render of the application. afterEveryRender(callback) runs after every render (it was called afterRender before Angular 20). Both:

  • Must be called in an injection context (a constructor or field initialiser), or pass an injector.
  • Run only in the browser. They are skipped on the server, so you do not need isPlatformBrowser checks.
  • Run after change detection finished, so the DOM is up to date.
  • Are cleaned up automatically with the component.

Phases. If several components read and write the DOM in separate callbacks, they can force the browser to recalculate layout again and again (layout thrashing). To avoid this, pass an object with phases. Angular runs all callbacks of one phase before moving to the next:

  1. earlyRead: read the DOM before writes (rarely needed).
  2. write: change the DOM (styles, classes, sizes).
  3. mixedReadWrite: the default when you pass a plain function. It may do both. Prefer the other phases.
  4. read: read the DOM after writes, for example getBoundingClientRect.

Each phase function can return a value that is passed to the next phase.

Versus lifecycle hooks: ngAfterViewInit also runs on the server and runs only for that component's view. A render hook runs after the whole application tree is stable, and not on the server. Use afterNextRender for browser-only DOM work, and ngAfterViewInit when you only need the component's own view. Do not set signals in a render hook without a plan: updating state triggers another render, and with afterEveryRender this can loop.

typescript
import { Component, ElementRef, afterNextRender, inject, signal } from '@angular/core';

export const order: string[] = [];

@Component({
  selector: 'app-measure-box',
  template: `<div class="box">Content</div><p>Height: {{ height() }}</p>`,
})
export class MeasureBoxComponent {
  private readonly host = inject<ElementRef<HTMLElement>>(ElementRef);
  readonly height = signal(0);

  constructor() {
    afterNextRender({
      write: () => {
        order.push('write');
        const box = this.host.nativeElement.querySelector('.box') as HTMLElement;
        box.style.height = '80px';
        return box; // passed to the next phase
      },
      read: (box) => {
        order.push('read');
        this.height.set(box.getBoundingClientRect().height);
      },
    });
  }
}

Angular 20.3, strict template check passes. A Karma test checks that the phase callbacks run in write then read order and that the size is read after the write.

Key points

  • `afterNextRender` runs once, `afterEveryRender` runs on every render, both browser only.
  • They run after the whole app has rendered, so DOM measurements are correct.
  • Phases (`earlyRead`, `write`, `mixedReadWrite`, `read`) prevent layout thrashing.
  • Use them for third party DOM libraries, focus and measurement. Avoid state changes that cause endless renders.

Common mistakes

  • Using `window` in a constructor and breaking server rendering.
  • Reading and writing the DOM in one callback in a loop, causing layout thrashing.
  • Setting a signal in `afterEveryRender` and creating a render loop.

Follow-ups

  • Why do render hooks not run on the server?
  • How do phases pass values to each other?
  • When would `ngAfterViewInit` still be the right hook?

Tips

  • Name the browser-only property first. It is the main reason.
  • Explain layout thrashing in one sentence.
  • Say that `afterRender` was renamed `afterEveryRender`.

Evaluation rubric

  • Explains when the hooks run and where.
  • Knows the phases and why they exist.
  • Compares correctly with lifecycle hooks.