Rezaul Karim
Rezaul Karim

Reputation: 1

Do we need afterViewInit lifecycle hook anymore in angular?

We know that the afterViewInit lifecycle hook gets triggered once when a component finally finishes the initialization of it's view. Thus if any component has to work with it's viewChild, this lifecycle hook get's it's relevance to be invoked inside the component.

But these days we don't have to depend on afterViewInit lifecycle as we can configure the viewchild with setting the "static" property to "true" to tell angular that we want to resolve the query result before change detection (if this was the case).

My question is, if this was the only purpose of ngAfterViewInit to write the logic that depends on component's viewchild, do we really need to use this lifecycle hook anymore if we can work the other way around?

Upvotes: 0

Views: 858

Answers (1)

Fateh Mohamed
Fateh Mohamed

Reputation: 21387

You can't always use static: true sometimes the visibility of the viewChild or the child component is dynamic means that it does change, for instance viewChild element where the visibility is based on a directive like *ngIf. you can't use static: true in that case

Upvotes: 1

Related Questions