question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug(nav): lifecycle events inside ion-nav fire incorrectly

See original GitHub issue

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x
  • Nightly

Current Behavior

I’ve come across with some strange behaviour, when you present a page using ModalControllerwhich wraps ion-nav.

Parent Page

  public async openModal() {
    const modal = await this.modalController.create({
      component: ModalOutletPage,
      componentProps: { component: AddCardPage },
    });

    await modal.present();
  }

Modal Outlet Page

export class ModalOutletPage implements OnInit {
  @Input() public component: HTMLElement;
  @Input() public componentProps: unknown;
}
<ion-nav [root]="component" [rootParams]="componentProps"></ion-nav>

in this test i console.log() ionViewDidEnter(), ionViewDidLeave(), ngOnInit(), ngOnDestroy() inside add-card.page.ts

First time opening modal:

on open

  1. ionViewDidEnter trigger

on close

  1. ngOnInit trigger
  2. ngOnDestroy trigger

second time opening modal:

on open

  1. ngOnInit trigger
  2. ionViewDidEnter trigger twice
  3. ionViewDidLeave trigger
  4. ngOnDestroy trigger

on close

  1. ngOnInit trigger
  2. ngOnDestroy trigger

https://user-images.githubusercontent.com/26873275/180785472-225256f1-c1d7-4e4c-a752-b37317ac55e9.mov

UPDATE

This is the behaviour, if i remove changeDetection: ChangeDetectionStrategy.OnPush inside modal-outlet.page.ts:

https://user-images.githubusercontent.com/26873275/180788325-724afcbb-c291-46ff-9a41-956a099e40fb.mov

Expected Behavior

Lifecycle events should trigger correctly

Steps to Reproduce

see video

Code Reproduction URL

https://github.com/DwieDima/ion-nav-lifecycle-bug

Ionic Info

Ionic CLI : 5.4.16 Ionic Framework : @ionic/angular 6.1.15 @angular-devkit/build-angular : 14.1.0 @angular-devkit/schematics : 13.2.6 @angular/cli : 14.1.0 @ionic/angular-toolkit : 6.1.0

Utility:

cordova-res : not installed native-run : not installed

System:

NodeJS : v16.14.2 npm : 8.5.0 OS : macOS Monterey

Additional Information

maybe related to #25638

Issue Analytics

  • State:open
  • Created a year ago
  • Reactions:4
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

6reactions
sean-perkinscommented, Jul 25, 2022

@DwieDima thanks for this issue. I’ve confirmed that the PR you have tag as being related, does not have an impact on this behavior.

We will log this as a bug and prioritize in the backlog. Thanks!

2reactions
akram-picommented, Aug 10, 2022

@DwieDima @sean-perkins I was able to fix the bug with this code:

export class ModalOutletPage implements OnInit {
  @Input() public component: HTMLElement;
  @Input() public componentProps: unknown;
  
  isModalInitialized: boolean = false;

  constructor() {
    this.fixModalLifecycle();
  }

  private fixModalLifecycle() {
    setTimeout(() => {
      this.isModalInitialized = true;
    }, 50);
  }
}
<ion-nav  *ngIf="isModalInitialized" [root]="component" [rootParams]="componentProps"></ion-nav>

BTW, smaller timeouts works!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Life cycle events of app.component.ts - angular - Stack Overflow
First time it fires constructor() code.But after that none of the lifecycle events are working. My scenario: I have implemented side menu as ......
Read more >
ionViewDidEnter is not invoked on leaving a pushed page
If you want to trigger an event while leaving a page you can use “ionViewDidLeave” event. For further reference ionic Lifecycle events.
Read more >
ionic-v1.x - Yarn - Package Manager
Bug Fixes. tap: Exclude contentEditable from preventDefault on tapMouseDown(). (5ac4549). history fix Back Button issue (980126d) ...
Read more >
signature=207c933289ebc5cfadf8cbb9de20798d,ionic ...
Bug Fixesbackdrop: nvda no longer incorrectly announces backdrop ... react: fire lifecycle events on initial render, fixes #20071 (2dcf3ee).
Read more >
IOnic 3.0.1 发布,紧随ng4 + 的脚步 - 掘金
This fixes a bug with ionicons getting styled incorrectly. ... nav: fire lifecycle events from app root portal (a4e393b); nav: fix menuCtrl ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found