Use 'active' state from React Router in Styled Components

前端 未结 9 1642
北海茫月
北海茫月 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-15 12:41

    The prop className is getting added to the children of NavLink and so its not accessible at NavLink level. The docs were not clear about this. Therefore, we cannot check for props.className === 'active' and add styles.

    Instead, you could just resort to css inside styled components for your use:

      const LinkElem = styled(NavLink)`
      // example style
      &.active {
        color: ${props => props.theme.orange }
      }
    `;
    

提交回复
热议问题