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: IonTabs needs a IonTabBar

See original GitHub issue

Bug Report

Ionic version:

[ ] 4.x [x] 5.x React

Current behavior: Conditionally rendering IonTabBar throws error.

Expected behavior: As commented in the error caller, the error is unnecessary and should be removed.

Location: /IonTabs.tsx:100
if (!tabBar) {
   // TODO, this is not required
 throw new Error('IonTabs needs a IonTabBar');
}

Steps to reproduce:

  1. Create a project with IonTabs and React,
  2. Comment out the IonTabBar.

Related code: Example conditional renderer:

const showTabNav = false;

...
<IonTabs>
 <IonRouterOutlet>
...
</IonRouterOutlet>
 {showTabNav && (
            <IonTabBar slot="bottom">
              <IonTabButton tab="tab1" href="/tab1">
                <IonIcon icon={mapOutline} />
                <IonLabel>Explore</IonLabel>
              </IonTabButton>
              <IonTabButton tab="tab2" href="/tab2">
                <IonIcon icon={homeOutline} />
                <IonLabel>Deals</IonLabel>
              </IonTabButton>
              <IonTabButton tab="tab3" href="/tab3">
                <IonIcon icon={walletOutline} />
                <IonLabel>Wallet</IonLabel>
              </IonTabButton>
            </IonTabBar>
          )}
</IonTabs>

Other information: N/A

Ionic info: N/A

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

8reactions
ptmkennycommented, Mar 9, 2021

I also got this error when I tried to move my <IonTabBar> into a component and call that component in the same location.

For example, based on the related code above:

<IonTabs>
 <IonRouterOutlet>
 </IonRouterOutlet>
 <AppTabBar />
</IonTabs>

And then the AppTabBar component:

            <IonTabBar slot="bottom">
              <IonTabButton tab="tab1" href="/tab1">
                <IonIcon icon={mapOutline} />
                <IonLabel>Explore</IonLabel>
              </IonTabButton>
              <IonTabButton tab="tab2" href="/tab2">
                <IonIcon icon={homeOutline} />
                <IonLabel>Deals</IonLabel>
              </IonTabButton>
              <IonTabButton tab="tab3" href="/tab3">
                <IonIcon icon={walletOutline} />
                <IonLabel>Wallet</IonLabel>
              </IonTabButton>
            </IonTabBar>
6reactions
piotr-czcommented, Dec 3, 2021

@ptmkenny

The problem is that the IonTabs component checks if at least one it’s direct children is type of IonTabBar (see https://github.com/ionic-team/ionic-framework/blob/v5.9.1/packages/react/src/components/navigation/IonTabs.tsx#L131-L138)

Workaround is to create Tab wrapper component which includes both IonTabs and IonTabBar

// TabsWrapper.tsx

const TabWrapper: React.FC<{
  items: IMenuItem[]
}> = ({
  items,
  children,
}) => (
  <IonTabs>
    {children}
    <IonTabBar slot="bottom">
      {items.map((item, index) =>
        <IonTabButton
          key={index}
          tab={index.toString()}
          href={item.url}
        >
          <IonIcon md={item.mdIcon} ios={item.iosIcon} />
          <IonLabel>{item.title}</IonLabel>
        </IonTabButton>
      )}
    </IonTabBar>
  </IonTabs>
)

export default TabWrapper

Usage:

// App.tsx

import TabsWrapper from './TabsWrapper'

const menuItems = [
  // Menu items
]

const App: React.FC = () => (
  <TabsWrapper items={menuItems}>
    <IonRouterOutlet id="main">
      {/** Routes */}
    </IonRouterOutlet>
  </TabsWrapper>
)

export default App
Read more comments on GitHub >

github_iconTop Results From Across the Web

bug: IonTabs needs a IonTabBar · Issue #21541 · ionic- ...
Current behavior: Conditionally rendering IonTabBar throws error. Expected behavior: As commented in the error caller, the error is unnecessary ...
Read more >
React + Ionic 5: Tab navigation problems
According to the React Tabs starter template you have to nest the IonRouterOutlet inside of IonTabs , which is nested in IonReactRouter ....
Read more >
Hiding Tab Bar for certain routes - Ionic Forum
I have no way of conditionally rendering it as any time the component is not rendered I get the error Error: IonTabs needs...
Read more >
How to mix Tabs and non-tab pages in Ionic React? : r/ionic
I'm looking to have a login page that's just a plain form as the starting point of the app, and once a user...
Read more >
CODEBEAT - Automated code review for mobile and web
packages/react/src/components/navigation/IonTabs.tsx ... import { IonTabBar } from './IonTabBar' ; ... throw new Error( 'IonTabs needs a IonTabBar' );.
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