Using styled-components with react-router
See original GitHub issuereact-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:
- Created 7 years ago
- Comments:32 (12 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
FWIW, this is working for me.
Ah I see yeah I misread
&.active
as&:active
in the original example. To be honest, I actually think turning&.active
intoactiveClassName
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:Lol, actually you can do this:
😂