Home > Software engineering >  Change icon on selected tab ionic react
Change icon on selected tab ionic react

Time:12-26

Is there any way to change the ionic icon on the selected tab?

.tsx

import { Redirect, Route } from "react-router-dom";
import {
  IonApp,
  IonIcon,
  IonLabel,
  IonRouterOutlet,
  IonTabBar,
  IonTabButton,
  IonTabs,
  setupIonicReact,
} from "@ionic/react";
import { IonReactRouter } from "@ionic/react-router";
import {
  homeOutline,
  notificationsOutline,
  personOutline,
  searchOutline,
} from "ionicons/icons";
import Tab1 from "./pages/Tab1";
import Tab2 from "./pages/Tab2";
import Tab3 from "./pages/Tab3";

/* Core CSS required for Ionic components to work properly */
import "@ionic/react/css/core.css";

/* Basic CSS for apps built with Ionic */
import "@ionic/react/css/normalize.css";
import "@ionic/react/css/structure.css";
import "@ionic/react/css/typography.css";

/* Optional CSS utils that can be commented out */
import "@ionic/react/css/padding.css";
import "@ionic/react/css/float-elements.css";
import "@ionic/react/css/text-alignment.css";
import "@ionic/react/css/text-transformation.css";
import "@ionic/react/css/flex-utils.css";
import "@ionic/react/css/display.css";

/* Theme variables */
import "./theme/variables.css";
import "./theme/custom.css";

setupIonicReact();

const App: React.FC = () => (
  <IonApp>
    <IonReactRouter>
      <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" id="dbf8q1Q3hEY9HZnyJesy">
          <IonTabButton tab="tab1" href="/tab1">
            <IonIcon icon={homeOutline}></IonIcon>
            <IonLabel>Home</IonLabel>
          </IonTabButton>
          <IonTabButton tab="tab2" href="/tab2">
            <IonIcon icon={searchOutline}></IonIcon>
            <IonLabel>Search</IonLabel>
          </IonTabButton>
          <IonTabButton tab="tab3" href="/tab3">
            <IonIcon icon={notificationsOutline}></IonIcon>
            <IonLabel>Notifications</IonLabel>
          </IonTabButton>
          <IonTabButton tab="tab4" href="/tab4">
            <IonIcon icon={personOutline}></IonIcon>
            <IonLabel>Profile</IonLabel>
          </IonTabButton>
        </IonTabBar>
      </IonTabs>
    </IonReactRouter>
  </IonApp>
);

export default App;

on the selected(Active) tab it should be: <IonIcon icon={home}></IonIcon> else <IonIcon icon={homeOutline}></IonIcon>

I just want to play only with the icons. Solution, please!

I have found an angular solution: Get selected tab to switch icons Ionic 5

How to use ionTabsDidChange in react?

CodePudding user response:

it's onIonTabsDidChange in react. you can use it like this:

<IonTabs onIonTabsDidChange={() => {...}}

but if you just want to change icon when it is selected, there is a better way with css that I use myself. ionic append the tab-selected class to the selected tab which is better to use when you just want to change looks. here is an example:

<IonTabButton tab="tab1" href="/tab1">
            <IonIcon icon={home} className="selected"></IonIcon>
            <IonIcon icon={homeOutline} className="unselected"></IonIcon>
            <IonLabel>Home</IonLabel>
</IonTabButton>

and then wherever you define your classes (for example variables.css):

.tab-selected .unselected {
  display: none;
}

.tab-selected .selected {
  display: initial !important;
}

.selected {
  display: none;
}
  • Related