Xpleria
Xpleria

Reputation: 5873

Render Callback for Angular Components

Is there a way to register a callback for whenever the component gets redrawn/re-rendered?

For eg. to count how many times the component was updated on the DOM?

I'm using OnPush change detection.

@Component({
  changeDetection: ChangeDetectionStrategy.OnPush,
  selector: 'app-my-component',
  templateUrl: './my-component.component.html',
  styleUrls: ['./my-component.component.scss']
})

But if I'm not wrong, when the inputs to the component are updated, the component gets automatically redrawn, isn't it?

Upvotes: 0

Views: 659

Answers (1)

Some random IT boy
Some random IT boy

Reputation: 8477

I believe you're mostly right and you want to hook yourself into ngOnChanges.

Quoting from their docs:

Respond when Angular sets or resets data-bound input properties. The method receives a SimpleChanges object of current and previous property values. Note that this happens very frequently, so any operation you perform here impacts performance significantly. See details in Using change detection hooks in this document.

Upvotes: 1

Related Questions