If you're using styled-components version 3.4.10, then you have to use injectGlobal instead of createGlobalStyle, because createGlobalStyle was only release in v4 of styled-components. Check it out: [Deprecated] injectGlobal
So, in order for your code to work, you have to change a few things:
import { injectGlobal } from 'styled-components';
const GlobalStyle = injectGlobal`
html {
height: 100%
}
* {
padding: 0;
margin: 0
}
`
export default GlobalStyle
And in your index.ts
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
import GlobalStyle from './theme/globalStyles'
ReactDOM.render(
<React.StrictMode>
<GlobalStyle /> // this comes first
<App />
</React.StrictMode>,
document.getElementById('root')
)
Answer from mindmaster on Stack OverflowVideos
The problem lies in the fact that there is currently an issue when using @import in Global Styles. The solution is to take the @import out and place it elsewhere, such as in a link tag, if you're using Google fonts.
The documentation is unclear when it comes to using @import within createGlobalStyle. There seems to a be a difference in production compared to development, most likely to stop FOUC, if you move the @import into the top-level then it will import correctly and display your styles.
import { createGlobalStyle } from 'styled-components';
const GlobalStyle = createGlobalStyle`
@import url("https://fonts.googleapis.com/css?family=Quicksand");
body {
color: red;
}
`
const App = () => (
<>
<GlobalStyle />
<Provider store={store}>
<Router>
<div>
//REST OF APP
</div>
</Router>
</Provider>
</>
);
export default App;
You can do it this way:
import { createGlobalStyle } from "styled-components";
import { Fonts } from "./fonts";
import { FontFamily } from "./fontFamily";
const GlobalStyle = createGlobalStyle`
${Fonts}
${FontFamily}
`;
If you have a lot of global styles like we did, you can use styled-components css method to leverage styled-components (css) IDE syntax highlighting & linting you can also do the following:
import React from 'react'
import { createGlobalStyle, css } from 'styled-components'
const Reset = css`
* {
box-sizing: border-box;
}
`
const Accessibility = css`
.hidden {
display: none !important;
visibility: hidden;
}
`
const BaseStyles = createGlobalStyle`
${Reset};
${Accessibility};
`
export const GlobalStyles = () => (
<>
<BaseStyles />
</>
)
Import GlobalStyles and render as sibling to
{children}