Question presented to candidate: "We need to initialise a third-party chart library on a div in our component and keep it in sync as data changes. Should that go in ngAfterViewInit, afterNextRender or afterRender? And is afterRender even still the right name?"
What a strong answer should cover:
- Naming: in Angular 20+
afterRenderwas renamedafterEveryRender; 22.2.3 does not exportafterRenderat all (importing it is a compile error). ngAfterViewInitis a class hook that runs during change detection, once per component, also on the server; the element exists but the app has not finished rendering.afterNextRenderruns once, after the whole application rendered, browser only: the right place to create the chart.afterEveryRenderruns after every application render: the place to push new data into the chart, preferably in awriteorreadphase.- Both render hooks must be registered in an injection context (constructor), or be given
{ injector }. - Writing a signal on every
afterEveryRendercauses an infinite loop (NG0103).
Clarifying questions expected:
- "Does the app use SSR or prerendering?"
- "Does the library need the element measured (layout), or just a node to attach to?"
Code / implementation expected: Yes — create the library object in afterNextRender, update it in afterEveryRender.