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
If you are using object syntax in styled-components, you can implement the next solution:
const activeClassName = 'nav-item-active'
export const StyledLink = styled(NavLink).attrs({
activeClassName,
})(props => ({
height: '20px',
width: '20px',
backgroundColor: 'green',
['&.' + props.activeClassName]: {
backgroundColor: 'red'
}
}))
You can check a live example in the next StackBlitz project:
https://stackblitz.com/edit/react-hcudxz