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.

Using styled-components with react-router

See original GitHub issue

react-router’s <Link> component and react-modal2’s <Modal> component accept additional classNames via separate properties. e.g. <Link> accepts activeClassName and <Modal> accepts backdropClassName and modalClassName. It would be great to have a way to style these using styled-components. Perhaps we could have something like:

const StyledLink = styled(Link)`
  color: palevioletred;
  display: block;
  margin: 0.5em 0;
  font-family: Helvetica, Arial, sans-serif;

  &:hover {
    text-decoration: underline;
  }
  &.active {
    color: red;
  }
`;

where &.active gets passed as a separate class name to the activeClassName property.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:32 (12 by maintainers)

github_iconTop GitHub Comments

22reactions
mikehazellcommented, Mar 6, 2017

FWIW, this is working for me.

import { Link } from 'react-router';
import styled from 'styled-components';

const NavItem = styled(Link)`
  background-color: blue;

  &.${(props) => props.activeClassName} {
    background-color: paleturquoise;
  }
`;

NavItem.defaultProps = {
  activeClassName: 'active',
};

export default NavItem;
18reactions
geelencommented, Nov 13, 2016

Ah I see yeah I misread &.active as &:active in the original example. To be honest, I actually think turning &.active into activeClassName is really clever, but it’s already a valid thing to do so I don’t want to overload it.

I suppose you could do &:pass-as-prop(activeClassName) { ... } or something else wacky like that. But tbh that’s starting to put a lot of magic in there for an API that… is kinda clunky.

IMO React Router could add a data-active attribute instead of applying a different classname, then we could do:

const StyledLink = styled(Link)`
  color: palevioletred;
  display: block;
  margin: 0.5em 0;
  font-family: Helvetica, Arial, sans-serif;

  &[data-active] {
    color: red;
  }
`;

Lol, actually you can do this:

const rando = btoa(Math.random())
const StyledLink = styled(Link)`
  color: palevioletred;
  display: block;
  margin: 0.5em 0;
  font-family: Helvetica, Arial, sans-serif;

  &.${rando} {
    color: red;
  }
`;
<StyledLink activeClassName={rando}/>

😂

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style React Router links with styled-components
How to style React Router links with styled-components · Prerequisites · Creating the React application · Deleting unwanted files in the src folder ......
Read more >
How to style your React-Router links using styled-components
METHOD-3: Styling React-Router links using 'NavLinks' and 'activeClassNames'. · activeClassName is an attribute that we can use to create a class ...
Read more >
Styling react-router-dom Link using styled-components getting ...
I pass props to my styled-components all the time. I don't know if the problem is that I'm styling a component Link instead...
Read more >
Basic implementation of React router and styled-components
Basic implementation of React router and styled-components. 2. Embed Fork Create Sandbox Sign in. Sandbox Info. Basic implementation of React router and ...
Read more >
Basics - styled-components
The styled method works perfectly on all of your own or any third-party component, as long as they attach the passed className prop...
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