ionic-framework icon indicating copy to clipboard operation
ionic-framework copied to clipboard

bug: react, IonTabBar does not allow for wrapped IonTabButton components

Open thepedroferrari opened this issue 3 years ago • 12 comments

Bug Report: Importing outer IonTabButton into IonTabBar does not render in React.

Ionic version: [x] 5.x (React)

Current behavior: I am following the docs on creating the tabbar with tab buttons which works perfectly:

const Tabs = () => {
  return (
    <IonReactRouter>
      <IonTabs>
        <IonRouterOutlet>
          <Routes />
        </IonRouterOutlet>
        <IonTabBar slot="bottom">
          <IonTabButton tab="settings" href="/settings">
            <IonIcon icon={cog} />
            <IonLabel>Settings</IonLabel>
          </IonTabButton>
          <IonTabButton tab="Login" href="/login">
            <IonIcon icon={triangle} />
            <IonLabel>Login / Register</IonLabel>
          </IonTabButton>
        </IonTabBar>
      </IonTabs>
    </IonReactRouter>
  )
}

Renders like it is supposed to:

Screenshot 2021-03-22 at 22 20 35

I particularly like breaking down components a bit more, so let's start with just one file:

// SettingsTab.tsx
import { IonIcon, IonLabel, IonTabButton } from "@ionic/react"
import { cog } from "ionicons/icons"

export const SettingsTab = () => (
  <IonTabButton tab="settings" href="/settings">
    <IonIcon icon={cog} />
    <IonLabel>Settings</IonLabel>
  </IonTabButton>
)

And import it and use it on my Tabs.tsx:

const Tabs = () => {
  return (
    <IonReactRouter>
      <IonTabs>
        <IonRouterOutlet>
          <Routes />
        </IonRouterOutlet>
        <IonTabBar slot="bottom">
          <SettingsTab />
          <IonTabButton tab="Login" href="/login">
            <IonIcon icon={triangle} />
            <IonLabel>Login / Register</IonLabel>
          </IonTabButton>
        </IonTabBar>
      </IonTabs>
    </IonReactRouter>
  )
}

Expected behaviour:

Now I would expect the behaviour to be the same as the previous one when I declared all buttons directly inside the Tabs, but this is what I am getting instead: Screenshot 2021-03-22 at 22 25 15

Steps to reproduce:

  • Install the latest Ionic React
  • Start a Tabs application
  • Create a new component that exports a single tab button, for example:
    export const SettingsTab = () => (
      <IonTabButton tab="settings" href="/settings">
        <IonIcon icon={cog} />
        <IonLabel>Settings</IonLabel>
      </IonTabButton>
    )
    
  • import that component into the tabs and place it within <IonTabBar></IonTabBar>

Thanks

thepedroferrari avatar Mar 22 '21 21:03 thepedroferrari

Thanks for the issue! This issue has been labeled as needs reproduction. This label is added to issues that need a code reproduction.

Please reproduce this issue in an Ionic starter application and provide a way for us to access it (GitHub repo, StackBlitz, etc). Without a reliable code reproduction, it is unlikely we will be able to resolve the issue, leading to it being closed.

If you have already provided a code snippet and are seeing this message, it is likely that the code snippet was not enough for our team to reproduce the issue.

For a guide on how to create a good reproduction, see our Contributing Guide.

ionitron-bot[bot] avatar Mar 22 '21 22:03 ionitron-bot[bot]

Here is a reproduction of the issue: https://github.com/thepedroferrari/Ionic-External-Tab

thepedroferrari avatar Mar 23 '21 22:03 thepedroferrari

Thanks, I can reproduce this issue. The problem is that when IonTabBar renders its child components, it filters out anything that is not IonTabButton: https://github.com/ionic-team/ionic-framework/blob/master/packages/react/src/components/navigation/IonTabBar.tsx#L221

liamdebeasi avatar Mar 24 '21 14:03 liamdebeasi

Nicely found!

thepedroferrari avatar Mar 24 '21 16:03 thepedroferrari

We're running into this as well, both from trying to componentize creating IonTabButtons (like above example), and also when trying to implement feature flags to conditionally show IonTabButton elements

J-Gonzalez avatar Jun 16 '21 03:06 J-Gonzalez

some issue.

shikelong avatar Jul 08 '21 08:07 shikelong

Any updates on this? @thepedroferrari did you manage to figure out any solution?

DeniKucevic avatar Sep 16 '21 09:09 DeniKucevic

Facing the same issue at the moment. Does anybody know of a workaround?

aleksei-tro avatar Jun 09 '22 08:06 aleksei-tro

@liamdebeasi Is there any update to this? I want to create a tab button based on user preference, but I have a lot of options. Therefore it makes sense to wrap the options in their own component and return the appropriate <IonTabButton>

playaz87 avatar Nov 07 '23 01:11 playaz87

+1 for this issue. I want to componentize my routing to make it easier to maintain and isolate logic.

jonathan-chin avatar Jan 25 '24 06:01 jonathan-chin