Use 'active' state from React Router in Styled Components

前端 未结 9 1640
北海茫月
北海茫月 2021-02-15 12:24

React Router adds an active class to NavLinks when you are on the page that they link to. How can I access this property with Styled Components. I need to styled me

9条回答
  •  情书的邮戳
    2021-02-15 12:34

    Styled component:

    import { NavLink } from 'react-router-dom';
    
    export const MyNavLink = styled(NavLink)`
      &.${props => props.activeClassName} {
        color: red;
      }
    `;
    

    Usage:

    Dashboard
    

    Tested with:

    react-router-dom 5.1.2
    styled-components 5.0.1

提交回复
热议问题