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.

[feature request] support styling Route element of 'react-router'

See original GitHub issue

It has been a bit tricky to apply global style with styled-components, and I don’t want to use css-loaders just for several lines of global css code, and ideally position I found to inject the global style is at the Router object of react-router, which works well with redux connect function at the moment, but doesn’t seem to respond to styled. Would it be possible to make it work here?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:20 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
epilandecommented, Jun 30, 2017

@zhenyulin in https://github.com/styled-components/styled-components/issues/958#issuecomment-312240894, I think injectGlobal isn’t working because you need to add a selector.

Something like this should work.

injectGlobal`
+  body {
     color: green;
+  }
`;

Also you should be able to use your theme in injectGlobal. Assuming your theme is a javascript object, you can import it through out your app and use it just like any object.

This is how my global.js looks like, then I import it in my app’s main entry point.

import { injectGlobal } from 'styled-components';
import fonts from './fonts';
import theme from './theme';

injectGlobal`
  html {
    background: ${theme.lightBackgroundColor};
  }

  body {
    font-family: ${theme.baseFont};
    color: ${theme.textColor};
  }

  ${fonts}
`;
1reaction
k15acommented, Jun 30, 2017

In theory, we could add this of course but I don’t think we should because we already have the tools to inject globals styles and it’s pretty easy so we shouldn’t add any more complexity.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to style React Router links with styled-components
Let's look at how to use styled-components and TypeScript to style React Router links in an application's navbar.
Read more >
Feature Overview v6.6.1 - React Router
React Router enables "client side routing". In traditional websites, the browser requests a document from a web server, downloads and evaluates CSS and ......
Read more >
Leveling up with React: React Router - CSS-Tricks
React Router's power comes in when we use multiple routes to define which component should render based on which path is currently active:...
Read more >
How to Handle 404 Requests and Active Styles in React Router
Switch: It helps us to render the Component only when particular path/URL matches. In above code, only two routes are there so that...
Read more >
React Router 6 Tutorial - Robin Wieruch
As you can see, it's possible to nest Route components in another Route component -- whereas the former become so-called Nested Routes. Now ......
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