1

I have a tab navigation page in an ionic react app and I want to add a side menu to this page, I know I have to use the IonMenu component but I am struggling to merge the two. here is my code indicating how I implemented my tab navigation:

import React, { Component } from "react";
 import {
  IonIcon,
  IonLabel,
  IonRouterOutlet,
  IonTabBar,
  IonTabButton,
  IonTabs,
} from "@ionic/react";

import { Redirect, Route } from "react-router-dom";
import { IonReactRouter } from "@ionic/react-router";
import { home, at, albums,addCircleOutline } from "ionicons/icons";

import Tab1 from "./Tab1";
import Tab2 from "./Tab2";
import Tab3 from "./Tab3";
import Tab4 from "./Tab4";

class Home extends Component {
  render() {
    return (
      <IonReactRouter>
        <IonTabs>
          <IonRouterOutlet>
            <Route path="/tab1" component={Tab1} exact={true} />
            <Route path="/tab2" component={Tab2} exact={true} />
            <Route path="/tab3" component={Tab3} />
            <Route path="/tab4" component={Tab4} />
            <Route
              path="/"
              render={() => <Redirect to="/tab1" />}
              exact={true}
            />
          </IonRouterOutlet>
          <IonTabBar slot="bottom">
            <IonTabButton tab="tab1" href="/tab1">
              <IonIcon icon={home} />
              <IonLabel>Home</IonLabel>
            </IonTabButton>
            <IonTabButton tab="tab2" href="/tab2">
              <IonIcon icon={albums} />
              <IonLabel>Saved</IonLabel>
            </IonTabButton>
            <IonTabButton tab="tab4" href="/tab4">
              <IonIcon icon={addCircleOutline} />
              <IonLabel>Request</IonLabel>
            </IonTabButton>
            <IonTabButton tab="tab3" href="/tab3">
              <IonIcon icon={at} />
              <IonLabel>About</IonLabel>
            </IonTabButton>
          </IonTabBar>
        </IonTabs>
      </IonReactRouter>
    );
  }
}

export default Home;

I need to know how can I add a side menu to this page and how can I add a button in such a way that when I click it, the button will open the side menu

2 Answers2

8

First, for all of your pages that you want to add into tab menu, set path that starts with /tabs/*, for example /tabs/tab1, /tabs/tab2, /tabs/tab3, etc.

You need to create one component with a name, for example, MainTabs that will have tab navigation and IonRouterOutlet that contain only routes for pages in the tabs navigation. It could look like this:

<IonTabs>
    <IonRouterOutlet>
      <Route path="/tabs/tab1" component={Tab1} exact/>
      <Route path="/tabs/tab2" component={Tab2} exact/>
      <Route path="/tabs/tab3" component={Tab3} exact/>
    </IonRouterOutlet>
    <IonTabBar slot="bottom">
      <IonTabButton tab="tab1" href="/tabs/tab1">
        <IonLabel>Tab 1</IonLabel>
      </IonTabButton>
      <IonTabButton tab="tab2" href="/tabs/tab2">
        <IonLabel>Tab 2</IonLabel>
      </IonTabButton>
      <IonTabButton tab="tab3" href="/tabs/tab3">
        <IonLabel>Tab 3</IonLabel>
      </IonTabButton>
    </IonTabBar>
  </IonTabs>

Then in App.tsx, you can add a typical side menu code but with one important route:

<Route path="/tabs" component={MainTabs} />

This router outlet doesn't have 'exact' prop and will pick up all routes that starts with /tabs/ and that is defined in router outlet in MainTabs component. So, App.tsx file can look something like this:

<IonReactRouter>
  <IonSplitPane contentId="main">
    <IonMenu contentId="main">
      <IonContent>
        <IonList>
          // here you can put your side menu items with router links
        </IonList>
      </IonContent>
    </IonMenu>
    <IonRouterOutlet id="main">
      // Important route for tabs navigation!
      <Route path="/tabs" component={MainTabs} />
      // and here are defined every other route in your app
      <Route path="/sideMenu1" component={SideMenu1} exact />
      <Route path="/sideMenu2" component={SideMenu2} exact />
      <Route path="/sideMenu3" component={SideMenu3} exact />
    </IonRouterOutlet>
  </IonSplitPane>
</IonReactRouter>

To add a side menu toggle button you need to use IonMenuButton component in the IonToolbar component. Example:

<IonPage>
  <IonHeader>
    <IonToolbar>
      <IonButtons slot="start">
        <IonMenuButton />
      </IonButtons>
      <IonTitle>My profile</IonTitle>
    </IonToolbar>
  </IonHeader>
  <IonContent className="ion-padding">
    // page content
   </IonContent>
</IonPage>

I hope this will help!

  • 1
    Also, please keep in mind to keep the tabs route i.e. `` on top of other routers inside App.tsx file. – Shahidur Dec 07 '20 at 21:08
  • How to make default `/tabs` also point to `/tabs/tab1` page? – RameshN Nov 03 '21 at 02:32
  • @RameshN i realize your comment was a long time ago, answering for anyone that might want to know. To make the default tab /tabs/tab1 you would just change the route path to /tabs/tab1 like this: `` – DevMike Mar 22 '22 at 16:53
2

Just wrap the whole thing with the split-pane.

<IonReactRouter>
  <IonSplitPane contentId="main">
    <Menu />
    <IonRouterOutlet id="main">
      <Route path="/page/:name" component={Page} exact />
      <Route path="/tabs" component={TabRootPage} />
      <Redirect from="/" to="/tabs" exact />
    </IonRouterOutlet>
  </IonSplitPane>
</IonReactRouter>

See example here - https://github.com/aaronksaunders/ionic-sidemenu-tabs-auth-starter

Aaron Saunders
  • 33,180
  • 5
  • 60
  • 80