Search code examples
javascriptcssreactjsstyled-components

Styled Component v5.3 createGlobalStyles not Working


I'm trying to create Global Styles for my application using createGlobalStyles but somehow the components are not rendering and I am getting a console warning.

App.js

import GlobalStyles from "./styles/GlobalStyles";

function App() {
  return (
    <div className="App">
      <GlobalStyles>
        <h1>This is a test line</h1>
      </GlobalStyles>
    </div>
  );
}

export default App;

GlobalStyles.js

import { createGlobalStyle } from "styled-components";

const GlobalStyles = createGlobalStyle`
html {
  font-size: 8px;
}
* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
  
  font-weight: 200;
}
body {
  font-size: 8px;
  
}
h1 {
    font-size: 2rem;
}

`;

export default GlobalStyles;

Console Error[enter image description here][1]

GlobalStyle.js:28 The global style component sc-global-dtGiqY was given child JSX. createGlobalStyle does not render children.

Error Screenshot

https://i.sstatic.net/QJBLz.png


Solution

  • Place <GlobalStyles /> as a sibling to your other content—not as a parent element trying to accept children.

    <div className="App">
      <GlobalStyles />
      <h1>This is a test line</h1>   
    </div>