I'm really banging my head against the wall with this one... :-\
I have a dumb component that with an @Input()
that receives an array:
export class ChildComponent implements OnChanges {
@Input() items: Item[];
ngOnChanges(changes: SimpleChanges) {
console.log('ngOnChanges fired:', changes['items'].currentValue.length)
}
}
The parent component passes the array to the child component via a template binding and the async
pipe:
<h1>Parent Component</h1>
<child [items]="items$ | async"></child>
My understanding is that changes should automatically be detected when using the async
pipe with @Input()
allowing them to be processed within the ngOnChanges
lifecycle hook.
Is this correct?
However, ngOnChanges
is not fired in the child component when items$
emits a new value.
I have a DataStore
class that maintains this observable array of items. As new items are fetched, it generates a new array (immutable) and pushes it through the items$
observable. However, the changes aren't detected in the child component. If I click around the page a bit, eventually change detection kicks in and it will update.
Here's the kicker: I created a sample of the problem on StackBlitz, only to find out it works fine there!!!!
So, I know there is something different my real code vs the simplified example, but I have not been able to track it down.
That is why my question is:
How to debug RxJS / Angular?
I've added logging to the
items$
inside the parent component and I can see the new values arriving just fine, but the child component is not receiving them. I can see thatngOnChanges
is not being fired within the child component.I thought maybe the
items$
observable reference was getting reassigned somewhere, leaving the template with a subscription to an observable that isn't actually getting new emissions, so I made the@Input()
a getter/setter can I could monitor each time it was set, and it only occurs once.I tried changing the child component to use the
OnPush
change detection strategy, but it had no effect.
I'm stumped...
Any suggestions on how I can track down my issue?
Thanks! :-)