Use 'active' state from React Router in Styled Components

前端 未结 9 1863
北海茫月
北海茫月 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:33

    As of react-router v4, you can style the active state of NavLink without any dependencies using activeClassName and Styled Components' attrs() function:

    export const StyledNavLink = styled(NavLink).attrs({
      activeClassName,
    })`
    
      &.${activeClassName} {
        background: red;
      }
    `;
    

    Related documentation:

    • activeClassName
    • attrs()

提交回复
热议问题