Skip to content

bug: ion-tab ionTabsWillChange event is fired after the tab component ngOnInit, ionViewWillEnter and ionViewDidEnter are fired #27212

Closed

Description

@ryaa

Prerequisites

Ionic Framework Version

v7.x

Current Behavior

ion-tab ionTabsWillChange event is fired after the tab component ngOnInit, ionViewWillEnter and ionViewDidEnter are fired

Expected Behavior

ion-tab ionTabsWillChange event must be fired BEFORE the tab component ngOnInit, ionViewWillEnter and ionViewDidEnter are fired

Steps to Reproduce

  1. open https://stackblitz.com/edit/angular-jddxdk-zlkkrz?file=src/app/example.component.html
  2. open console
  3. navigate between tabs
    Note that the ion-tab ionTabsWillChange event is fired after the tab component ngOnInit, ionViewWillEnter and ionViewDidEnter are fired - see below

Screenshot 2023-04-17 at 3 13 01 PM

Code Reproduction URL

https://stackblitz.com/edit/angular-jddxdk-zlkkrz

Ionic Info

See https://stackblitz.com/edit/angular-jddxdk-zlkkrz

Additional Information

None

Activity

  1. changed the title [-]bug: [/-] [+]bug: ion-tab ionTabsWillChange event is fired after the tab component ngOnInit, ionViewWillEnter and ionViewDidEnter are fired[/+] on Apr 17, 2023
  2. self-assigned this
    on Jul 5, 2023
  3. sean-perkins commented on Jul 5, 2023

    @sean-perkins
    Contributor

    Hello @ryaa thanks for reporting this issue!

    Can you explain why ionTabsWillChange should be emitted before the other lifecycle events? In the current implementation, the event fires after the active tab is set (either manually through the API or from a user interaction). It isn't directly coupled to the child component's lifecycle.

    This was a recent change here: #24910

    This was done in response to this issue: #18900

  4. added
    needs: reply
    on Jul 5, 2023
  5. ryaa commented on Jul 10, 2023

    @ryaa
    Author

    Can you explain why ionTabsWillChange should be emitted before the other lifecycle events? In the current implementation, the event fires after the active tab is set (either manually through the API or from a user interaction). It isn't directly coupled to the child component's lifecycle.

    The problem is that, in each tab component ngOnInit method, I need to get the active/selected tab and this is not available yet (as this is set in ionTabsWillChange which fired after the tab child component life cycle events invoked). My assumption, based on the event name ionTabsWillChange (note that it will change), this should happen before the tab components are loaded/initialized.

  6. added and removed
    needs: reply
    on Jul 10, 2023
  7. sean-perkins commented on Jul 10, 2023

    @sean-perkins
    Contributor

    @ryaa appreciate the follow-up. I agree the event ordering here is not correct. The ion-tabs implementation is listening for the result from stackEvents, but this is emitted after the child component is mounted in the DOM and ngOnInit resolves.

    ion-router-outlet should emit another event prior to setting the active view, allowing ion-tabs to emit ionTabsWillChange with the selected tab before it is activated. I do not believe our original resolution in #24910 was correct after reviewing this again.

    Can you share your ideal implementation for accessing the selected tab from within the ngOnInit? Currently the child components would not have access to the ion-tabs event emission, even if we changed the ordering of when they fired. This data would need to be stored in a provider and shared between the component rendering ion-tabs and the inner child component.

  8. added
    package: angular
    type: bug
    on Jul 10, 2023
  9. ryaa commented on Jul 12, 2023

    @ryaa
    Author

    Can you share your ideal implementation for accessing the selected tab from within the ngOnInit? Currently the child components would not have access to the ion-tabs event emission, even if we changed the ordering of when they fired. This data would need to be stored in a provider and shared between the component rendering ion-tabs and the inner child component.

    Here is the sample showing a simplified implementation that I have in my application now - see https://stackblitz.com/edit/angular-jddxdk-qrddgc?file=src%2Fapp%2Fhome%2Fhome-page.component.ts

    Note that each tab component has a workaround in ngOnInit to set the correct value for the activeTab in Share provider. Ideally, this should be set once in onTabsWillChange in the main tabs page, so that all the individual tab child components etc. can read the correct activeTab value from Share provider.

    I hope that it makes sense. Please let me know if you have any questions or comments. Thank you

  10. added
    type: bug
    and removed
    type: bug
    on Jul 17, 2023
  11. sean-perkins commented on Aug 14, 2023

    @sean-perkins
    Contributor

    Hello @ryaa, would you be available to test this dev-build and let me know if it resolves your issue?

    npm install @ionic/[email protected]
    

    Forked reproduction with the dev-build: https://stackblitz.com/edit/angular-jddxdk-rnznpn?file=package.json

    Thanks!

  12. ryaa commented on Aug 15, 2023

    @ryaa
    Author

    Hello @ryaa, would you be available to test this dev-build and let me know if it resolves your issue?

    sure. I will try to test in this week

  13. added a commit that references this issue on Aug 18, 2023
    bbfb8f8
  14. ionitron-bot commented on Sep 17, 2023

    @ionitron-bot

    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.

  15. locked and limited conversation to collaborators on Sep 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

package: angulartype: bug

Type

No type

Fields

No fields configured for issues without a type.

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions