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.

[Button] No way to style a button based on the active NavLink from react-router in v5 whereas in v4 it was possible

See original GitHub issue

Duplicates

  • I have searched the existing issues

Latest version

  • I have tested the latest version

Current behavior 😯

In MUI4 we can do like this,

const useStyles = makeStyles({
  button: {
    "&.active": {
      background:'black',
    },
  },
});
<Button
  className={classes.button}
  component={NavLink}
  to="/page-link"
>

But in MUI5 makeStyles is deprecated and also passing as component to Button is not setting the active class to the button when the link is active.

Expected behavior 🤔

MUI5 button should set the classes set by the component passed to it.

<Button
  className={classes.button}
  component={NavLink}
  to="/page-link"
>

The above should add the active class when the link is active.

Steps to reproduce 🕹

Steps:

Context 🔦

No response

Your environment 🌎

`npx @mui/envinfo`
  System:
    OS: macOS 12.0.1
  Binaries:
    Node: 16.14.2 - ~/.nvs/default/bin/node
    Yarn: 1.22.5 - ~/.yarn/bin/yarn
    npm: 8.5.0 - ~/.nvs/default/bin/npm
  Browsers:
    Chrome: 100.0.4896.127
    Edge: Not Found
    Firefox: 96.0.2
    Safari: 15.1
  npmPackages:
    @emotion/react: ^11.9.0 => 11.9.0 
    @emotion/styled: ^11.8.1 => 11.8.1 
    @mui/base:  5.0.0-alpha.75 
    @mui/icons-material: ^5.6.2 => 5.6.2 
    @mui/material: ^5.6.0 => 5.6.0 
    @mui/private-theming:  5.6.0 
    @mui/styled-engine:  5.6.0 
    @mui/system:  5.6.0 
    @mui/types:  7.1.3 
    @mui/utils:  5.6.0 
    @types/react:  17.0.43 
    react: ^18.0.0 => 18.0.0 
    react-dom: ^18.0.0 => 18.0.0 
    styled-components: ^5.3.5 => 5.3.5 
    typescript:  4.6.3 

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
siriwatknpcommented, Sep 18, 2022

Thanks. But this does not seem to resolve the issue:

My bad, from your snippet it should be &.active not &:active.

Can you try this?

import { NavLink as ReactNav } from 'react-router-dom'

<ListItemButton 
      to="/dashboard" 
      component={ReactNav}
      sx={{
        '&.active': {
          color: 'blue',
          fontWeight: 600
        }
      }}
    >
      <ListItemIcon>
        <DashboardIcon />
      </ListItemIcon>
      <ListItemText primary="Dashboard" />
</ListItemButton>
1reaction
dizzyjaguarcommented, Sep 27, 2022

Thanks. But this does not seem to resolve the issue:

My bad, from your snippet it should be &.active not &:active.

Can you try this?

import { NavLink as ReactNav } from 'react-router-dom'

<ListItemButton 
      to="/dashboard" 
      component={ReactNav}
      sx={{
        '&.active': {
          color: 'blue',
          fontWeight: 600
        }
      }}
    >
      <ListItemIcon>
        <DashboardIcon />
      </ListItemIcon>
      <ListItemText primary="Dashboard" />
</ListItemButton>

This is the best solution! Just ran into the same problem.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style React Router links with styled-components
Let's look at how to use styled-components and TypeScript to style React Router links in an application's navbar.
Read more >
Wrapping a react-router Link in an html button - Stack Overflow
I was wondering if there is a way to wrap a Link element from 'react-router' in an HTML button tag using react. I...
Read more >
#4 Style Active button using NavLink and Change ... - YouTube
In this video you will learn the beginner approach to using HTML, Tailwindcss and React hooks to change the backgroundColor on button click....
Read more >
Active NavLink Classes with React Router - Ultimate Courses
From here we can an active class, or multiple classes, based on the isActive value. We can also supply inactive classes for when...
Read more >
NavLink - React Router: Declarative Routing for React.js
In React Router v6, activeClassName will be removed and you should use the function className to apply classnames to either active or inactive...
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