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.

[Tabs] Conditionally rendering of `Tab` not working properly

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Steps to reproduce 🕹

Steps:

  1. Add some tabs
<Tabs
  value={value}
  onChange={handleChange}
  aria-label="basic tabs example"
>
  <Tab label="Item One" {...a11yProps(0)} />
  <Tab label="Item Two" {...a11yProps(1)} />
  <Tab label="Item Three" {...a11yProps(2)} />
</Tabs>
  1. Wrap them in a component that checks if the tab should be displayed, for simplicity I am just retuning the children wrapped in a fragment but in theory it should make some checks to return the children or null
const Authorizer = ({ children }: { children: React.ReactNode }) => (
  <>{children}</>
);
<Authorizer>
  <Tab label="Item One" {...a11yProps(0)} />
</Authorizer>

https://codesandbox.io/s/unruffled-thunder-f405xf?file=/demo.tsx

Current behavior 😯

Tabs stop working, they can be clicked but don’t navigate to the tab panel

Expected behavior 🤔

Tabs should work normally as there is not being introduced anything additional in the tree

Context 🔦

I am trying to show or hide the tabs depending on the user permissions, for this I am using an Authorizer component that returns the children (wrapped in a fragment) or null depending on if the user has the right permissions but by doing so, the tabs stop working correctly.

Your environment 🌎

npx @mui/envinfo
  System:
    OS: macOS 12.6
  Binaries:
    Node: 16.15.0 - ~/.nvm/versions/node/v16.15.0/bin/node
    Yarn: 1.22.18 - ~/.nvm/versions/node/v16.15.0/bin/yarn
    npm: 8.5.5 - ~/.nvm/versions/node/v16.15.0/bin/npm
  Browsers:
    Chrome: 106.0.5249.103
    Edge: Not Found
    Firefox: Not Found
    Safari: 16.0
  npmPackages:
    @emotion/react: ^11.9.3 => 11.9.3 
    @emotion/styled: ^11.9.3 => 11.9.3 
    @mui/base:  5.0.0-alpha.85 
    @mui/icons-material: ^5.8.4 => 5.8.4 
    @mui/lab: ^5.0.0-alpha.91 => 5.0.0-alpha.91 
    @mui/material: ^5.8.4 => 5.8.4 
    @mui/private-theming:  5.8.4 
    @mui/styled-engine:  5.8.0 
    @mui/system:  5.8.4 
    @mui/types:  7.1.4 
    @mui/utils:  5.8.4 
    @mui/x-data-grid: ^5.12.3 => 5.12.3 
    @types/react: ^17.0.33 => 17.0.44 
    react: ^17.0.2 => 17.0.2 
    react-dom: ^17.0.2 => 17.0.2 
    typescript: ^4.5.4 => 4.6.3 

Using Chrome

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
mnajdovacommented, Dec 22, 2022

The reason is because you are not propagating all props to the underlaying Tab component. The Tabs uses the React.cloneElement API for providing props to the children it receives. Here is a working example: https://codesandbox.io/s/quizzical-hawking-q6ws58?file=/demo.tsx

Another useful example in terms of customization would be: https://mui.com/material-ui/react-tabs/#customization I hope this explanation helps.

If someone wants to extend the docs adding more information around this, would be great. cc @samuelsycamore

0reactions
AChangXDcommented, Oct 26, 2022

How I’m creating/passing the <Tabs>: setToolbarItems( <> <Tabs value={selectedTab} // If I get rid of this value field, things will work again but it won't display which tab is selected. onChange={(event, newValue) => { setSelectedTab(newValue); }} > <Tab label="Overview"></Tab> <Tab label="Sales Logs"></Tab> </Tabs> </> );

I’m making the <AppBar> dynamically change its content based on the page it’s on. On certain pages, it will display an <Tabs> for navigation within that page.

Code for <AppBar>:

`return ( <> <Box sx={{ minHeight: 64, flexGrow: 1 }}> <AppBar position=“static” sx={{ paddingLeft: 0, paddingRight: 0 }} > <Toolbar sx={{ minHeight: 64, margin: 0, justifyContent: ‘center’, backgroundColor: ‘white’, }} > {showLeftNav && ( <IconButton edge=“start” color=“inherit” aria-label=“menu” sx={{ paddingLeft: 0, width: 64 }} onClick={() => { setShowLeftNav((previousState) => !previousState); }} > <MenuIcon /> </IconButton> )}

        {toolbarItems}                        <- where the <Tabs> end up

        {showLeftNav && <Box sx={{ width: 64 }}></Box>}
      </Toolbar>
    </AppBar>
  </Box>
</>

);`

Read more comments on GitHub >

github_iconTop Results From Across the Web

[Tabs] Can't conditionally render tabs! · Issue #406 - GitHub
I wan't, for an example, to be able to hide both the Tab button and its TabPanel when the user doesn't have the...
Read more >
Tab navigation using conditional rendering - Stack Overflow
I'm loading tab contents using reactjs conditional rendering. I have done it for 2 tabs successfully. But now i have a need of...
Read more >
Allow Conditional Tabs for Lightning App Builder Tab ...
Currently, there is a workaround to achieve this effect: You can create multiple Tabs components, each with conditions.
Read more >
How to check which tab is active using Material UI
It's time for your code editor. Create a file named TabsExample.js in your src folder and paste the following code into it. Javascript ......
Read more >
How to create tabs on streamlit that work independently
tabs, conditional rendering of content is currently not supported. You could try using the st.radio with the horizontal option to act as tabs....
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