Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bug: ion-nav does not fire change detection for the root page when using OnPush change detection #21423

Closed
ekrapfl opened this issue Jun 2, 2020 · 5 comments · Fixed by ionic-team/ionic-docs#1448
Labels

Comments

@ekrapfl
Copy link

ekrapfl commented Jun 2, 2020

Bug Report

Ionic version:
[x] 4.11.1 (also reproduced in sample in 5.1.1)

Current behavior:
I am presenting a modal using the ModalController. The modal presents a simple component that contains only an <ion-nav> with a root component set on it. This is so that I can create a sort of "guided process" within the modal using the <ion-nav> to navigate between pages of the process. The root page that I am setting on <ion-nav> uses OnPush change detection and contains some observable state that is shown in the UI asynchronously. If I set the modal component's ChangeDetectionStrategy to Default, everything works the way I expect with the component set as the root on the <ion-nav>. However, if I use ChangeDetectionStrategy.OnPush for the modal component, the root page will never display my asynchronous state changes. In fact, it will not even fire the regular angular lifecycle events like ngOnInit. It does fire the ionic lifecycle events, but the angular ones are never called.

Expected behavior:
I would expect that my modal component containing the <ion-nav> should be able to utilize whatever ChangeDetectionStrategy I choose, and as long as the child component can handle it (and mine can), it should fire angular lifecycle events and update the view accordingly.

Steps to reproduce:
I have put together a sample application that has a single button to launch a modal. That modal displays a page using ChangeDetectionStrategy.OnPush. The root component loaded in the <ion-nav> should show text initially, wait 3 seconds, and then update the text. It will actually never show the text at all with the current setup. Switching the ModalComponent to ChangeDetectionStrategy.Default will make it all work as expected.

Related code:
Sample app: https://github.com/ekrapfl/nav-on-push
There are comments in src/app/home/process.component.ts and src/app/home/modal-page.component.ts to indicate some of the things I have attempted/noticed.

Ionic info:

Ionic CLI                     : 5.4.16 (/Users/krapfl/.npm/_npx/19146/lib/node_modules/ionic)
Ionic Framework               : @ionic/angular 5.1.1
@angular-devkit/build-angular : 0.901.7
@angular-devkit/schematics    : 9.1.7
@angular/cli                  : 9.1.7
@ionic/angular-toolkit        : 2.2.0
@ionitron-bot ionitron-bot bot added the triage label Jun 2, 2020
@mhartington
Copy link
Contributor

Hmm, ok so there's a few things here. It's not 100% due to Ionic, but it is something we should probably document.

Basically, the property binding for <ion-nav root=""> is set but the actual CD had been be pretty much turned off. What you need to do is call a private method from angular to do this.

import {
  Component,
  NgModule,
  ChangeDetectionStrategy,
  ɵmarkDirty as markDirty,
} from '@angular/core';
....

export class ProcessComponent {
  ModalPageComponent = ModalPageComponent;
  ionViewDidEnter(){
    markDirty(this)
  }
}

It might make sense to not use onPush here in the ProcessComponent since it needs to be able to handle this situation.

@ekrapfl
Copy link
Author

ekrapfl commented Jun 3, 2020

That is actually what we ended up doing. It didn't seem like a terrible practice to do that for our situation, as there really is little to nothing in the process component, but we thought it was worth raising the issue in case you were not aware. I am fine with it if the answer is to just not use OnPush in that situation. Some documentation to that effect would definitely be nice, though.

Thanks Mike!

@liamdebeasi
Copy link
Contributor

Thanks for the issue! I added some clarification to the docs via ionic-team/ionic-docs#1448.

@ekrapfl
Copy link
Author

ekrapfl commented Jun 4, 2020

Works for me. Thanks!

@ionitron-bot
Copy link

ionitron-bot bot commented Jul 4, 2020

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

@ionitron-bot ionitron-bot bot locked and limited conversation to collaborators Jul 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants