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: IonRouterOutlet fails to work when dynamically adding Routes after the first render

See original GitHub issue

Bug Report

Ionic version: 5.0.7

Current behavior: When the app first loads and <IonRouterOutlet> is rendered all the routes will not re-render even when they are changed. For instance, several routes/redirects get changed or added after a user logs in, but the IonRouterOutlet will not update routes. I can see the new routes updated using the inspector, but IonRouterOutlet seems to ignore them as if it has cached them someplace and then ignores any updates.

Expected behavior: When routes are added or removed from IonRouterOutlet at run time, the new routes should become available and the removed ones should no longer exist.

Steps to reproduce: Dynamically generate a series of routes from an array of route data. Run an event like a redux hook update that will cause the App component to re-render and in this process make the routes change. The bug is that the application ignores the route changes.

Related code:

–Main App–

const App: React.FC = () => {
  const loggedIn = useSelector(SelectLoginStatus);
  const currentUser = useSelector(SelectCurrentUser);

  return (
    <IonApp>
      {console.log(AppRoutesNoAuthRoutes(loggedIn, currentUser))}}
      <IonReactRouter>
        <IonSplitPane contentId="main">
          <Menu />
         <IonRouterOutlet key="AppIonRouterOutlet" id="main">
            {AppRoutes(loggedIn, currentUser)}
          </IonRouterOutlet>
        </IonSplitPane>
      </IonReactRouter>
    </IonApp>
  );
};

export default App;

–Route Rendering function–

export const AppRoutes = (loggedIn:boolean, currentUser:IUser) => {
    routeInfoArray:IRouteInfo[] = myInfoAray;
    keyPrefix:string = "uniqueKeyPrefix";

    return routeInfoArray.map((route, i) => {
        if(route.roles.filter(role => role === UserRoles.Unregistered).length) {
            return <Route key={`${keyPrefix}-${i}`} path={route.path} component={route.component} exact />
        } else {
            if(loggedIn) {
                if(route.roles.filter(role => role === currentUser.Role || role === UserRoles.AllRegistered).length) {
                    return <Route key={`${keyPrefix}-${i}`} path={route.path} component={route.component} exact />
                } else {
                return <Redirect key={`${keyPrefix}-${i}`} from={route.path} to={AccountRouteInfo[AccountRouteKeys.Unauthorized].path} />
                }
            } else {
                return <Redirect key={`${keyPrefix}-${i}`} from={route.path} to={AccountRouteInfo[AccountRouteKeys.Login].path} />
            }
        }        
    });
}

–Notes– I have noticed that if I nest my routes in a component and then update like this IonRouteOutlet will allow them to be updated. However, nesting routes in another component causes other problems to happen because this does not appear to be allowed with IonRouteOutlet. I think I need IonRouteOutlet to make the IonSplitPane to work with multiple pages.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
elylucascommented, Jun 22, 2020

Hi @pjschrock,

We have a dev release that should fix this issue. 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/react@5.3.0-dev.202006222125.df37029 @ionic/react-router@5.3.0-dev.202006222125.df37029

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

Thanks!

0reactions
ionitron-bot[bot]commented, Jul 1, 2021

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

IonRouterOutlet fails to work when dynamically adding Routes ...
Bug Report Ionic version: 5.0.7 Current behavior: When the app first loads and is rendered all the routes will not re-render even when...
Read more >
Dynamic routing is not working while adding a new post
js ' which navigates the view path. All I have the issue is that loading error which I got during the dynamic route...
Read more >
React Navigation - Ionic Framework
A common routing use case is to provide a "fallback" route to be rendered in the event the location navigated to does not...
Read more >
Ionic Routing and Navigation Guide - Fireship
Ionic v4 Routing Basics with Angular · 0. Pro Tip - Install the Augury Chrome Plugin · 1. Define the Route in the...
Read more >
Shallow Routing - Next.js
You can use shallow routing to change the URL without triggering a new page ... Always do navigations after the first render router.push('/?counter=10', ......
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