New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New NavLink's className
syntax when used with styled-components
#8161
Comments
This isn't an issue with React Router. styled-components isn't forwarding the className prop as a function. There's nothing we can do on our end to fix this. |
@timdorr We are facing the exact same issue in several projects. We are mainly using the emotion style library and styled components. |
@wojtekmaj Here is our first workaround from our code bases. Maybe this helps you more than "the others are to blame".
There are still some hacky hacks and casts for typescript to accept that workaround. So maybe building a NavLink Component from scratch using hooks would be more effective. Update: Here is an example for a custom active Link from the docs: |
This is pretty annoying, but there is a quick way to get an active class, I'll paste the code I'm using (after spending an hour trying to find a way to make emotion & react-router isActive play nice)..
Where "sidebarData" is for example:
Ideally styled components / emotion wouldn't transform the function passed to className into a string, and possibly there are smarter ways to do this, but for getting the job done this works. Hope it helps a fellow googler on a deadline ;) |
This really bothers me as well. React Router shouldn’t be so opinionated that it breaks styled-components or other popular ecosystem libraries and conventions. Nowhere else is I’m guessing it’s too late to revert this decision, but are there any new official recommendations for how to go about solving this, which does not involve manually checking for string equality? Edit: Found a simpler solution over in this styled-components issue: Just apply styles using the export const Tab = styled(NavLink)`
…
&[aria-current] {
color: red;
}
` |
Active |
Not really sure under which repo to file this - while technically it's styled-components not recognizing the pattern, it's React-Router that introduced this non-standard way of handling className prop in the first place.
Version
v6.0.0-beta.7
Test Case
Steps to reproduce
Read through the 1st example provided. There are 6 links created using styled-components
styled.a
:NavLink
,NavLink
and active.In each group, one of them is pink thanks to additional className "pink".
Now, open the 2nd example provided, which was upgraded to React-Router v6. There's only one important change in this example:
and poof! It doesn't work, as styled-component stringifies the function.
Expected Behavior
Actual Behavior
The text was updated successfully, but these errors were encountered: