Skip to content

bug: Nested IonRouterOutlet prevents component unmount #20597

Closed

Description

@OoDeLally

Bug Report

Ionic version:

5.0.1

Here is a repo with a minimal code to reproduce the bug:
https://github.com/OoDeLally/ionic-nested-router-outlet-bug

Ionic info:

▶ ionic info 

Ionic:

   Ionic CLI       : 5.4.16 (/home/pascal/.nvm/versions/node/v8.16.0/lib/node_modules/ionic)
   Ionic Framework : @ionic/react 5.0.1

Utility:

   cordova-res                          : not installed
   native-run (update available: 0.3.0) : 0.2.9

System:

   NodeJS : v8.16.0 (/home/pascal/.nvm/versions/node/v8.16.0/bin/node)
   npm    : 6.4.1
   OS     : Linux 4.15


Activity

  1. added
    package: react
    on Feb 24, 2020
  2. OoDeLally commented on Mar 9, 2020

    @OoDeLally
    Author

    Hi, any news of this?
    I've tried to investigate checking the source code, but it seems beyond my level of understanding.

  3. emindeniz99 commented on Mar 23, 2020

    @emindeniz99

    All of these issue links show the same problem that I am having trouble with. I replaced IonRouterOutlet with Switch, but IonTabs requires IonRouterOutlet, and I must delete IonTabs, and the app looks like ugly.
    Probably, the problem is caused by IonRouterOutlet.
    #20844
    #20597
    #20298
    Duplicate of #20298 #20597 #20844

  4. und3f1ned commented on Apr 6, 2020

    @und3f1ned

    Anybody? This is a huge blocker for me

  5. OoDeLally commented on Apr 6, 2020

    @OoDeLally
    Author

    For me too... So far the workaround we use is to force a complete page reload (window.location = '...').
    But this is definitely ugly, as the whole page flickers and we lose the parent state.

  6. und3f1ned commented on Apr 6, 2020

    @und3f1ned

    window.location

    No, God, please, no :) Try this #20707 (comment)

  7. OoDeLally commented on Apr 7, 2020

    @OoDeLally
    Author

    window.location

    No, God, please, no :) Try this #20707 (comment)

    It doesn't work for us.
    When using <Switch>, the parent component successfully unmounts, but it also unmounts when it should not (when the path changes but should still keep the parent component mounted), leading to a blank page, without any kind of console error.

    Staying with window.location = '...' for now, regarless of how ugly it is.

  8. und3f1ned commented on Apr 7, 2020

    @und3f1ned

    window.location

    No, God, please, no :) Try this #20707 (comment)

    It doesn't work for us.
    When using <Switch>, the parent component successfully unmounts, but it also unmounts when it should not (when the path changes but should still keep the parent component mounted), leading to a blank page, without any kind of console error.

    Staying with window.location = '...' for now, regarless of how ugly it is.

    Did you try Ionic lifecycle methods?
    Especially this one
    useIonViewDidLeave(() => { console.log('ionViewDidLeave event fired'); });

  9. OoDeLally commented on Apr 7, 2020

    @OoDeLally
    Author

    Did you try Ionic lifecycle methods?
    Especially this one
    useIonViewDidLeave(() => { console.log('ionViewDidLeave event fired'); });

    I just did. None of the 4 events in https://ionicframework.com/docs/react/lifecycle was fired.

  10. royvangeel commented on Apr 14, 2020

    @royvangeel

    I'm also experiencing the same issue, no lifecyle method will be fired. Tried both class and functional component using the ionic docs.

  11. elylucas commented on Apr 14, 2020

    @elylucas
    Contributor

    Hello all,

    I'm looking into this now, hope to have something to report on soon.

  12. elylucas commented on Jun 23, 2020

    @elylucas
    Contributor

    Hi All,

    We have a dev release that provides better support for nested IonRouterOutlet components. There is a new prop on IonRouterOutlet that lets the router know it is a nested outlet to better perform the transition.

    If your'e outlet is a nested outlet directly rendered by a Route in a parent outlet, then use the ionPage prop on the IonRouterOutlet. Here is an example:

    App.tsx:

    const App: React.FC = () => (
      <IonRouterOutlet>
        <Route path="/sub1" component={Sub1Outlet}  />
        <Route path="/sub2" component={Sub2Outlet} />
      </IonRouterOutlet>
    );
    

    Sub1Outlet.tsx:

    const Sub1Outlet: React.FC = () =>  (
        <IonRouterOutlet ionPage>
          <Route path="/sub1" exact={true} 
            render={() => <Redirect to="/sub1/page" />} />
          <Route path="/sub1/page" component={Page1} exact={true} />
        </IonRouterOutlet>
      );
    

    Sub2Outlet.tsx:

    const Sub2Outlet: React.FC = () =>  (
        <IonRouterOutlet ionPage>
          <Route path="/sub2" exact={true} 
            render={() => <Redirect to="/sub2/page" />} />
          <Route path="/sub2/page" component={Page2} exact={true} />
        </IonRouterOutlet>
      );
    

    If you can, could you try to install it and let us know if it fixes the issue and if you run into any others? To install it run:

    npm i @ionic/[email protected] @ionic/[email protected]

    If all goes good this should be available in the next Ionic release.

    Thanks!

  13. mateuskb commented on Aug 4, 2020

    @mateuskb

    It didn't unmount the component for me. For now I will stick with routerDirection="back". Although it's not correct, it's working the way I need

  14. 7 remaining items

  15. basicBrogrammer commented on Nov 10, 2020

    @basicBrogrammer

    routerDirection

    I added routerDirection back to any link going to the given route that i wanted to "remount"

  16. wibed commented on Dec 8, 2020

    @wibed

    what is the state on this issue?
    Nested routes result in breaking third party libraries (replacing ionRouterOutlet with Switch works)
    anyways navigation is crucial

  17. allanyego commented on Dec 8, 2020

    @allanyego

    what is the state on this issue?
    Nested routes result in breaking third party libraries (replacing ionRouterOutlet with Switch works)
    anyways navigation is crucial

    You have to remember that replacing IonRouterOutlet with a Switch will mean converting all your useIonViewDidEnter with useEffects and lose animations.

  18. emindeniz99 commented on Jan 7, 2021

    @emindeniz99

    Is there any new news?

  19. MiguelHernandezCh commented on Jan 24, 2021

    @MiguelHernandezCh

    I found a workaround.

    You can set an id to the <IonPage>. When you click the header button, the app will go back and remove your page from the DOM.

    This is to force unmount the page, and because this page is no longer on the DOM it cannot execute its life cycles.

    This is my helper function

    export const removePageFromDOM = (pageId: string) => {
        const element = document.getElementById(pageId)
        setTimeout(()=>{
            return element && element.remove()
        }, 1000) // Give a little time to properly unmount your previous page before removing the old one
    }
    

    This is my page

       return (
            <IonPage id="PageId">
                  ...
            </IonPage>
        );
    
    

    This is my custom header

    const Header: React.FC<Props> = ({title}) => {
        const history = useHistory();
    
        const handleBack = ()=>{
            history.goBack();
            removePageFromDOM("PageId")
        }
    
        return (
            <IonHeader>
                <IonToolbar>
                        <IonButtons slot="start">
                            <IonButton onClick={handleBack}>
                                <IonIcon slot="icon-only" ios={chevronBack} md={arrowBack}/>
                            </IonButton>
                        </IonButtons>
                    <IonTitle>{title}</IonTitle>
                </IonToolbar>
            </IonHeader>
        );
    };
    

    Hope this helps

  20. jamt0 commented on Feb 24, 2021

    @jamt0

    This is a huge blocker for me.

  21. ItayElgazar commented on Sep 19, 2021

    @ItayElgazar

    Any updates?

  22. ashleydavis commented on Jan 21, 2022

    @ashleydavis

    It seems to me that Ionic just don't deal with these issues.

  23. e-kotsiuk commented on Aug 16, 2022

    @e-kotsiuk

    In case of Vue framework, adding key attribute to "ion-router-outlet" resolved it for me

    <ion-router-outlet id="main-content" :key="route.fullPath" ></ion-router-outlet>

  24. Mati-Educa commented on Mar 27, 2023

    @Mati-Educa

    I found a workaround. I'm just using the React router feature to go to another page.

    import { useHistory } from 'react-router-dom';
    let history = useHistory();
    
    //in your link or button
    history.replace('your page');
    

    That code triggers an unmounted event.

    I hope this workaround is helpful to you.

  25. jeffvandyke commented on Mar 2, 2024

    @jeffvandyke

    Stumbled across this issue today, found that links and ion items accept a routerOptions property with unmount: true that seems to take care of it.

  26. ionitron-bot commented on Mar 2, 2025

    @ionitron-bot

    Thanks for the issue! This issue is being closed due to inactivity. 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.

    Thank you for using Ionic!

  27. locked and limited conversation to collaborators on Mar 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    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