liveBook · Manning

afterNextRender

As explained in Modern Angular

afterNextRender is an Angular function that registers a callback to run once on the client after server-side rendering and the subsequent render.

afterNextRender registers a callback that runs once on the client after server-side rendering and the subsequent render. It prevents browser-only APIs such as window, document, and location from executing during SSR. Angular introduced it alongside afterRender in v16; use afterRender when the callback must run after every render. section 9.2.3

Client-only scroll listener

A scroll-to-top component registers its window scroll listener inside afterNextRender. The listener toggles button visibility after scrolling beyond 100 pixels, while the component uses ChangeDetectorRef to update the view. Because registration occurs once, the listener can operate independently afterward.

How Modern Angular covers it

How deep it goes
Distinguishes one-time and repeated callbacks, then introduces optional injector and rendering-phase configuration.
Examples
Uses a scroll-to-top button that installs a window scroll listener and updates visibility after scrolling.
What it assumes
Assumes familiarity with Angular components, SSR, lifecycle methods, and browser globals.
What you can do after
Apply it via Angular to defer browser-only callbacks safely until client rendering completes.
How it connects
Connects SSR, component lifecycle methods, afterRender, DOM manipulation, and dependency injection.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern Angular. The section links point to the parts of the book it draws on.