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: IonRouterOutlet with routerLink does not unmount routes #20543

Closed
OoDeLally opened this issue Feb 18, 2020 · 9 comments
Closed

bug: IonRouterOutlet with routerLink does not unmount routes #20543

OoDeLally opened this issue Feb 18, 2020 · 9 comments
Labels

Comments

@OoDeLally
Copy link

OoDeLally commented Feb 18, 2020

Bug Report

Ionic version:

5.0.0

Current behavior:

Leaving a page by clicking on <IonButton routerLink=".."> does not unmount the route's component.

Expected behavior:

The route component should be unmounted.

Steps to reproduce:

const Page: React.FC = () => {
  useEffect(
    () => {
      console.log('Mount Page');
      return () => {
        console.log('Unmount Page'); // Never called
      }
    },
    [],
  );
  return (
    <IonPage>
      <IonHeader>
        <IonToolbar>
          <IonButtons slot="start">
            <IonButton routerLink="/">Back</IonButton>
          </IonButtons>
          <IonTitle>Page</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent>
        Page
      </IonContent>
    </IonPage>
  );
};

const MainMenu: React.FC = () => {
  return (
    <IonPage>
      <IonHeader>
        <IonToolbar>
          <IonTitle>MainMenu</IonTitle>
        </IonToolbar>
      </IonHeader>
      <IonContent>
        <IonButton routerLink="/page/foo">Go to Page</IonButton>
      </IonContent>
    </IonPage>
  );
};

export const App: React.FC = () => (
  <IonApp>
    <IonReactRouter>
        <IonRouterOutlet>
          <Route path="/" component={MainMenu} exact={true} />
          <Route path="/page" component={Page} />
        </IonRouterOutlet>
    </IonReactRouter>
  </IonApp>
);

export default App;

Other information:

Ionic info:

▶ ionic info

Ionic:

   Ionic CLI       : 5.4.4 (/home/user/.nvm/versions/node/v8.16.0/lib/node_modules/ionic)
   Ionic Framework : @ionic/react 5.0.0

Utility:

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

System:

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



@ionitron-bot ionitron-bot bot added the triage label Feb 18, 2020
@OoDeLally
Copy link
Author

Similar/equal to #19984

@elylucas
Copy link
Contributor

This is by design. please see https://ionicframework.com/docs/react/navigation#ionrouteroutlet

@OoDeLally
Copy link
Author

Hello @elylucas
I realized that it does unmount if in addition to routerLink="...", I use routerDirection="back", then the page is effectively unmounted.
This is quite an unexpected behavior.
The doc mentions:
Each of these components also have a routerDirection prop to explicitly set the type of page transition to use ("back", "forward", or "none").
It is not obvious to me that this above implies which values unmount the component or not. May I suggest to make it extremely clear? I was banging my head for several hours on that problem.

@elylucas
Copy link
Contributor

Yep, I will look into that.

The idea is if you are leaving a page by going back, the state of the page no longer needs to be maintained. Contrast that by navigating between tabs or going forward from a list page to a details page, you want the same scroll position, form inputs, etc.. in place, so we keep those around in the dom and show/hide them via css.

@OoDeLally
Copy link
Author

OoDeLally commented Feb 18, 2020

Yes, this makes sense. However the default behavior is misleading.
imho not providing routerDirection should unmount the component, while a purposeful routerDirection="forward" should keep the component.

Now, a large part of my app uses programmatical redirections, with history.replace. history.replace does not seem to trigger an unmount. Any way I can achieve these routerLink-like transitions programmatically?

Thanks a lot

@elylucas
Copy link
Contributor

Ya, you can pass in the direction as a part of the state object (2nd param to replace):

history.replace('', { direction: 'forward'}) // or back / none

@OoDeLally
Copy link
Author

OoDeLally commented Feb 19, 2020

@elingerojo Thank you.
It doesnt seem to work if the page is accessed via a redirection.

This doesnt work for both version of the Redirect:

<Route path={ origin } exact render={ () => <Redirect to={ target } /> } />

or

<Redirect from={ origin } exact to={ target } />

This problem appears for version 4.11.5, 4.11.10 as well as 5.0.0.

@OoDeLally
Copy link
Author

OoDeLally commented Feb 24, 2020

It seems that this problem has nothing to do with redirections, but nested IonRouterOutlets.
Made a new issue for this problem #20597

@ionitron-bot
Copy link

ionitron-bot bot commented Mar 25, 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 Mar 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants