Sub topic 13
Render hooks for DOM work after rendering
This chapter assumes you know the AfterView hooks, basic change detection, and how Angular components are created in the browser and on the server. It covers afterNextRender and afterRender, the hooks for DOM work that must run only in the browser.
Why render hooks exist
Lifecycle methods such as ngAfterViewInit run during change detection. They also run during server-side rendering (SSR), where there is no real browser DOM. Code that measures an element, focuses an input, or starts a charting library can fail there, because window and layout information do not exist on the server.
Render hooks solve this. Angular runs them only in the browser, after it has finished updating the DOM.
The two hooks
afterNextRenderruns its callback once, after the next time Angular renders.afterRenderruns its callback after every render, for as long as the component or directive exists.
Both are functions, not methods you implement. You call them in an injection context, which in practice means the constructor or a field initializer. Angular removes the callback when the component is destroyed, so you do not clean it up yourself.
import { Component, afterNextRender } from '@angular/core';
@Component({
selector: 'app-measure',
standalone: true,
template: `<div>Content to measure</div>`,
})
export class MeasureComponent {
constructor() {
afterNextRender(() => {
console.log('Rendered in the browser; safe to touch the DOM');
});
}
}On the server this callback never runs. In the browser it runs once, after the first render.
Where they fit in the order
Render hooks run after change detection has updated the DOM. That is later than ngAfterViewChecked.
Choosing between them
| Need | Use |
|---|---|
| One-time setup, such as creating a chart or focusing an element | `afterNextRender` |
| Re-measure or re-sync the DOM after every update | `afterRender` |
| Work that also must run during SSR | A normal hook such as `ngOnInit` |
Common errors
- Calling outside an injection context. Calling
afterNextRenderinside a click handler throws an error. Register the hook in the constructor, or pass aninjectoroption when you must register it later. - Using
afterRenderfor one-time work. It runs after every render, so a chart created inside it is created again and again. UseafterNextRenderinstead. - Doing heavy work in
afterRender. It runs very often. Keep it cheap, and avoid changing state in it, which can trigger another render and another callback. - Expecting the callback in a test. Render hooks run after rendering, so call
fixture.detectChanges()orawait fixture.whenStable()before you assert on their effects.
Exercise
A component creates a third-party chart inside afterRender, and the chart appears duplicated after the data changes. What is the cause, and what is the fix?
Show answer
afterRender runs after every render, so a new chart is created each time Angular updates the DOM. Create the chart once in afterNextRender. If it must react to later changes, update the existing chart object instead of creating another one.