Tuesday, May 22, 2018

SimpleChanges is not simple.

Following up on this and this, something like this at an Angular 4 component...

ngOnChanges(changes: SimpleChanges) {
   console.log(changes.myInput);
}

 
 

...is going to log what is in an input like so...

@Input() myInput: any;

 
 

...when it changes and otherwise log undefined to the console. The undefined will show up even if the Input is hydrated, but not hydrated at that particular moment, that is: set earlier but not of concern RIGHT NOW! Just today I saw some head exploding magic wherein this.myInput (not changes.myInput but this.myInput) would survive a truthy check (and allow for code in an if statement to be run) inside of ngOnChanges twice without the SimpleChanges at the method signature and yet only once with it there! This was with ChangeDetectionStrategy.OnPush in the mix too. This would suggest that SimpleChanges can warp the nature/performance for ngOnChanges beyond merely providing metadata! Shocking shit!

No comments:

Post a Comment