Question presented to candidate:
"A parent pushes items into an array and edits keys on a settings object, both passed as inputs to an OnPush child, and the child never notices. What are IterableDiffers and KeyValueDiffers, how would you use them here, and would you still design it this way in Angular 22?"
What a strong answer should cover:
- Inputs are compared by reference, so in-place mutation does not fire
ngOnChangesor mark an OnPush child; differs compare contents instead. IterableDiffers(arrays, Sets, any iterable) andKeyValueDiffers(objects, Maps) are injectable factory registries:find(value).create(trackBy?)gives a stateful differ.differ.diff(current)returns null when nothing changed, otherwise a change record withforEachAddedItem,forEachRemovedItem,forEachMovedItem,forEachIdentityChange(iterable) orforEachChangedItem(key-value).- Typical home:
ngDoCheck, followed bymarkForCheck()or a signal write so an OnPush view refreshes; a trackBy function defines item identity. - Used internally by
NgForOf,NgStyleandKeyValuePipe; the built-in@foruses its own reconciliation withtrack. - Modern alternative: immutable updates or signals make most manual diffing unnecessary; differs run on every check, so cost matters.
Clarifying questions expected:
- "Can we change the parent to pass new references instead of mutating?"
- "Is the child OnPush, and is the parent checked when it mutates?"
Code / implementation expected: Yes β a child that reports what changed in a mutated array and object using both differs.