# Lifecycle hooks for inner components

**URL:** <https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095>\
**Category:** ionic-v3\
**Created:** [November 3, 2016, 7:27pm UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095 "2016-11-03T19:27:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![simevictor](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/simevictor/32/104396_2.png) [@simevictor](https://forum.ionicframework.com/u/simevictor)\
**Post date:** [November 3, 2016, 7:27pm UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095/1 "2016-11-03T19:27:25Z")

</div>

Hi,  
I am learning Ionic 2 now and I finding it hard to find out how to solve this issue.

I have a page that every time a user enter, it gets and updates data from the server using ionViewWillEnter hook. I want to separate this feature on a new component to be used in other pages.

This new component will be used by inserting his element tag on the pages. The problem is that ionViewWillEnter (neither other hooks) is not called in this component. It seems to be called only in Page Components. I change the code to use Angular 2 hook ngOnInit and the component works fine, but this hook is not called again when user navigates between pages.

Is there any way to refresh this component by calling a specific method whenever ionViewWillEnter was called on the PageComponent.

Thanks.

---

<div class="post-metadata">

**Author:** ![janein](https://avatars.discourse-cdn.com/v4/letter/j/f17d59/32.png) [@janein](https://forum.ionicframework.com/u/janein)\
**Post date:** [January 10, 2017, 4:17pm UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095/2 "2017-01-10T16:17:23Z")

</div>

Hi,

I have a similar issue.  
I want to get rid of listeners I set **inside my component**.  
If it would be a page I could just use **ionViewWillLeave** but unfortunately these methods do not get triggered inside components.

I got close to what I want to achieve by listening to the events on the navController like that:

```
this.nav.viewDidLeave.first().subscribe(() => {
 // .. do stuff ..
});

```

But these “leave-events” get triggered for leaving the last page too. So it kills my listeners (which are set inside ngOnInit) right after they are set.

Any ideas how to get a one-time hook fore leaving views **inside a component**?

---

<div class="post-metadata">

**Author:** ![neo\_split](https://avatars.discourse-cdn.com/v4/letter/n/ea5d25/32.png) [@neo\_split](https://forum.ionicframework.com/u/neo_split)\
**Post date:** [May 28, 2017, 10:29am UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095/3 "2017-05-28T10:29:20Z")

</div>

I am running into same problem here where in my custom component I need functionality of ionViewWillEnter() and ionViewWillLeave(). Angular2 lifecycle hooks dont really provide that.

Wondering how did you guys solve this?

One hack I can think of is to change my component to a IonicPage. As far as I see, there isnt much difference between the two. Any downside to this?

---

<div class="post-metadata">

**Author:** ![xme](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/xme/32/100523_2.png) [@xme](https://forum.ionicframework.com/u/xme)\
**Post date:** [June 1, 2017, 4:16pm UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095/4 "2017-06-01T16:16:26Z")

</div>

Hi!  
An inner component knows nothing about Ionic lifecycle events. Ionic/pages will not notify inner components about loading, become the active page, etc. If you need your inner component to do something based on lifecycle events, tie the parent lifecycle event with the inner component using Angular’s `@ViewChild` decorator.

In parent component get a reference to the child component using `@ViewChild` as explained [here](https://stackoverflow.com/questions/35140674/calling-a-function-in-a-child-angular2-component). Then, inside the parent lifecycle event, call a public function defined in child component.

Hope it helps,  
Xavi

---

<div class="post-metadata">

**Author:** ![Amoioo](https://avatars.discourse-cdn.com/v4/letter/a/94ad74/32.png) [@Amoioo](https://forum.ionicframework.com/u/Amoioo)\
**Post date:** [January 31, 2021, 9:05pm UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095/5 "2021-01-31T21:05:58Z")

</div>

Please describe how can tie the parent lifecycle event with the inner component using Angular’s @ViewChild decorator.

---

<div class="post-metadata">

**Author:** ![mhartington](https://sea1.discourse-cdn.com/ionicframework/user_avatar/forum.ionicframework.com/mhartington/32/137757_2.png) [@mhartington](https://forum.ionicframework.com/u/mhartington)\
**Post date:** [July 19, 2021, 8:52pm UTC](https://forum.ionicframework.com/t/lifecycle-hooks-for-inner-components/69095/6 "2021-07-19T20:52:14Z")

</div>

Closed on July 19, 2021, 8:52pm UTC


