1

I have tried from different ways to create nested routes with Tabs, the idea would be the following:

  • WelcomePage->LoginPage->HomePage(Tab)->RecipePage

The problem is in the entrance animation to homepage and also the departure, in the case of Welcomepage to LoginPage if it works, also to go backwards from recipepage to homepage.

I know that the matter lies in IonrouterOutlet and Route, and as these are organized, but I have made me the combinations that have occurred to me, and using several ways to render the component in the route, as is the render, component, and also as Children. If someone has done something similar and has gotten something optimal, or maybe it's happening something overlooked, I would appreciate help, thank you.

This is my app.tsx (Summary, I have more routes in addition /signup, about 50 routes):

      <IonApp>
        <IonReactRouter>
          <IonContent> // This Content only makes sense here because, the white space
                         // which replaces the animation turns black, blank looks better.
            <IonRouterOutlet>
              <Route path="/home"> //La ruta de las tabs
                <Home />
              </Route>
              <Route path="/signUp" exact>
                <SignUp />
              </Route>
             </IonRouterOutlet>
          </IonContent>
        </IonReactRouter>
      </IonApp>

My Home.tsx (4 routes in total):

    <IonTabs>
      <IonRouterOutlet>
        <Route path="/home/search" exact>
          <Search />
        </Route>
        <Route path="/home/explore" exact>
          <Explore />
        </Route>
        <Redirect exact path="/home" to="/home/search" />
      </IonRouterOutlet>
      <IonTabBar slot="bottom" className="py-1">
        <IonTabButton tab="search" href="/home/search">
          <IonIcon icon={searchOutline} />
          <IonLabel>{textos["page_buscar"]}</IonLabel>
        </IonTabButton>
        <IonTabButton tab="explore" href="/home/explore">
          <IonIcon icon={navigateOutline} />
          <IonLabel>{textos["page_explorar"]}</IonLabel>
        </IonTabButton>
      </IonTabBar>
    </IonTabs>
  • A more graphic sample, is reached to appreciate a white screen, that's where it should being the animation, this is on Android.

enter image description here

  • On iphone the animation of PageRecipesList(/recipes) to PageRecipe(/recipe) (no tabs):

enter image description here

  • On iphone the animation of PageHomeSearch(Tab home/search) to PageRecipesList(/recipes):

enter image description here

  • If you take off the that you comment above would look like this:

enter image description here

Thanks!

EDIT

The same that i want to do, but in only one file:

app.tsx:

const App: React.FC = () => (
  <IonApp>
    <IonReactRouter>
      <IonRouterOutlet>
        <Route exact path="/login">
          <Login />
        </Route>
        <IonTabs>
          <IonRouterOutlet>
            <Route exact path="/tab1">
              <Tab1 />
            </Route>
            <Route exact path="/tab2">
              <Tab2 />
            </Route>
            <Route path="/tab3">
              <Tab3 />
            </Route>
            <Route exact path="/">
              <Redirect to="/tab1" />
            </Route>
          </IonRouterOutlet>
          <IonTabBar slot="bottom">
            <IonTabButton tab="tab1" href="/tab1">
              <IonIcon icon={triangle} />
              <IonLabel>Tab 1</IonLabel>
            </IonTabButton>
            <IonTabButton tab="tab2" href="/tab2">
              <IonIcon icon={ellipse} />
              <IonLabel>Tab 2</IonLabel>
            </IonTabButton>
            <IonTabButton tab="tab3" href="/tab3">
              <IonIcon icon={square} />
              <IonLabel>Tab 3</IonLabel>
            </IonTabButton>
          </IonTabBar>
        </IonTabs>
      </IonRouterOutlet>
    </IonReactRouter>
  </IonApp>
);

The idea is do this: login->tab1, and tab1->login.

Jamt0
  • 131
  • 9
  • Those `IonTabButton` components, should they be rendering `Link` components of some sort and using a `to` prop versus an `href` of an anchor tag? You tagged this question with `react-router` and `react-router-dom` and neither of these use `href` attributes for anything. – Drew Reese Apr 26 '21 at 02:47
  • Ionic navigation works using react-router-dom, these components are from the Ionic framework based on said library, that's why the label with that Tag, the topics are closely related. – Jamt0 Apr 26 '21 at 03:17
  • 1
    I fully understand and am aware of that fact, but I'm saying they use `Link` components with `to` prop, not anchor tags or buttons with `href` attribute. Where are you applying route transition animations? So far nothing in your snippet, to me, implies an issue with `react-router` or `react-router-dom`. Perhaps something from the [Animated Transitions](https://reactrouter.com/web/example/animated-transitions) example can help. – Drew Reese Apr 26 '21 at 03:21
  • The animations are default of the Ionic framework through the IonReactRouter and IonRouterOutlet components, my problem is that the animations work partially, in certain routes they don't. I don't want to add any new animations, thanks anyway. – Jamt0 Apr 26 '21 at 03:28
  • 1
    Ok, got it. Removing unrelated tags then. – Drew Reese Apr 26 '21 at 03:32

2 Answers2

2

Have a look at how Ionic solve this in their Ionic React Conference Starter app, in particular this snippet from App.tsx and this snippet from their tabs component, MainTabs.tsx.

From your example code, the following changes should fix the animation issues:

  • In App.tsx, remove IonContent
  • In App.tsx, use the render method prop instead of nesting the Home component (e.g. <Route path="/home" render={() => <Home />} /> instead of <Route path="/home"><Home /></Route>)
  • Similarly, use the render method prop in Home.tsx, the component with the tabs
  • In the tabbed pages' components (Search and Explore in your example) make sure to use IonPage as this is needed to achieve animated navigation (see docs), e.g.
<IonPage>
  <IonHeader>...</IonHeader>
  <IonContent>...</IonContent>
</IonPage>

In general, follow Ionic's architecture as explained in their docs and as demonstrated in their example app and it should just work.

Alex Steinberg
  • 1,426
  • 13
  • 25
  • Hello, thanks for answering, everything you told me I had already tried before, and I have also already seen several examples, the code is like this because I have tried several things to achieve the correct animation, I have also tried to create a new project and try to do it Same as in the example, but I can't, I'm not sure, but the only way I've managed to do it is by installing certain versions of dependencies, as I have in this [repository](https://github.com/jamt0/JamTabsReactIonic), and in this repository it didn't matter if I used the component attribute, render or as a child. – Jamt0 Apr 30 '21 at 22:13
  • 1
    Are you trying to animate the transitions between tabs? If so, this is not good practice. The native platforms don't do this nor does ion-tabs. Animating these transitions would imply hierarchy but each tab is a sibling of each other. – Alex Steinberg May 02 '21 at 15:02
  • I try to make work the animations of the transitions between routes that are in a tab, and others that are not in a tab, I only have a component of tabs in my application – Jamt0 May 02 '21 at 15:09
  • Please look at this [codesandbox](https://codesandbox.io/s/nervous-rhodes-oi9pj), it is a simpler replica of what I want to achieve, I created this project from the ionic react base for tabs, there you can see that the animation between tab1 to login does not work, since for a moment it looks white , while login to tab1 if it works fine. – Jamt0 May 02 '21 at 15:40
  • 1
    If you create a separate tabs component like the ionic conference app instead of the ionic tabs app, as in this [codesandbox](https://codesandbox.io/s/youthful-moon-d1eg8), it should work. – Alex Steinberg May 02 '21 at 23:10
  • If you use the developer tools, in performance, you can see how the animation does not really work, it is clearer to see if you use IOS to debug. – Jamt0 May 03 '21 at 01:58
  • [Imagen](https://drive.google.com/file/d/1kI7CZ1RNufN7HIzHtY9xHo0-7hVaDwvd/view?usp=sharing) where I test the codesandbox you made. – Jamt0 May 03 '21 at 02:01
0

Alex Steinbergs answer was extremely helpful to me. Thank you!

I was struggling with similar issues for days. I was nesting the routes the same way. But finally I noticed there was a version mismatch in my package.json

Make sure you have the same versions of @ionic/react, rect-router, react-router-dom and @ionic/react-router as in the example app above, otherwise various bugs appear.

// package.json

"dependencies": {
   ...
  "@ionic/react": "^6.1.15",
  "@ionic/react-router": "^6.1.15",
  "react-router": "^5.2.0",
  "react-router-dom": "^5.2.0",
}

Also I'd recommend to study the differences between render and component props on <Route> component. Unwanted re-renders may also cause flickers.

react router difference between component and render

kube23
  • 11
  • 4