Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

React router link messes up MUI tab component styling

I’m having certain styling for the <Tab> component from MUI and currently I’m wrapping this tab with a <Link> from react-router-dom. The <Tab> has some styling which handles the active tab etc. but the Link messes this up.

What is the cleanest way to make sure the <Link> styling gets removed and it displays the <Tab> styling instead?

Code is as follow:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<Link to='/app/listings'>
   <Tab value="one" icon={<FormatListBulletedIcon />} label="Challenges" iconPosition='start' />
</Link>`

edit:

The <Tab> is styled in my MUI Theme as followed:

components: {
        MuiTab: {
            styleOverrides: {
                root: {
                    minHeight:24,
                    fontSize: 12,
                    padding: '6px 10px',
                    justifyContent: 'flex-start'
                },
            },
        },
   },

The <Link tag wrapping it overwrites this.

Simple example: https://codesandbox.io/s/confident-framework-u01mdk?file=/src/App.js

Remove the Link tag and styling changes.

>Solution :

You can render the Tab component as a Link.

Instead of

<Link color="inherit" to="/app/listings">
  <Tab
    value="one"
    icon={<FormatListBulletedIcon />}
    label="Challenges"
    iconPosition="start"
  />
</Link>

enter image description here

use the Tab component’s component prop to really render the Link component and pass all the link props to the Tab.

<Tab
  component={Link}
  color="inherit"
  to="/app/listings"
  value="one"
  icon={<FormatListBulletedIcon />}
  label="Challenges"
  iconPosition="start"
/>

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading