Question presented to candidate: "A child component needs to react when its parent passes it new data. Walk me through ngOnChanges: when does it run, what exactly is inside the SimpleChanges object, and when does it NOT fire even though you expected it to?"
What a strong answer should cover:
ngOnChangesruns when a parent template binding produces a new value for an input, compared by reference (===); the first call happens beforengOnInit.SimpleChangesholds oneSimpleChangeper changed input, withpreviousValue,currentValue,firstChange(andisFirstChange()); on the first callpreviousValueisundefined.- Only bound inputs that changed appear; an input nobody binds never shows up.
- It does not fire for in-place mutation of an object input, for the same value set again, or for code that assigns the property directly;
ComponentRef.setInput()does trigger it. - With OnPush as the default in v22, a mutated object input does not even re-render the child.
- Signal inputs are included in
ngOnChangestoo, butcomputed()/effect()are usually the better tool;SimpleChanges<T>gives typed keys.
Clarifying questions expected:
- "Are these decorator
@Inputs or signalinput()s?" - "Is the parent passing objects it mutates, or new objects each time?"
Code / implementation expected: Yes β a child that logs its SimpleChanges, plus a demonstration of a mutation that does not trigger it.