Styled Components
Styled Components is a popular JavaScript library for styling React components using tagged template literals and CSS. It allows developers to write actual CSS code to style components, abstracting away the styling into reusable, themeable components. Currently at stable version 6.4.0, it maintains an active release cadence with frequent patch releases and minor updates addressing bugs, performance, and compatibility with new React features like Server Components (RSC) and React 19. Key differentiators include its powerful theming capabilities, robust server-side rendering support, and the ability to automatically prefix CSS, making it a powerful alternative to traditional CSS modules or utility-first CSS frameworks. It aims to provide a seamless developer experience by keeping styling close to the component logic.
Common errors
-
ReferenceError: React is not defined
cause This typically occurs when using the CommonJS build of styled-components in a Node.js environment where React is not globally available or correctly imported within the module context.fixEnsure you are using an up-to-date version of styled-components (>=6.3.12). If still encountering this, verify your build system correctly handles CJS imports or consider migrating to ESM if possible. -
TypeError: Cannot read properties of undefined (reading 'document')
cause This error surfaces in React Native or other non-browser environments where the `document` object is not present, indicating that the web-specific styled-components bundle is being used.fixEnsure your build system is correctly resolving the React Native specific entry point for styled-components (e.g., using `platform` specific extensions or aliases). Update to styled-components@6.3.12 or newer for better internal handling of `document` references in RN. -
Warning: Prop `className` did not match. Server: "sc-kIeHzt" Client: "sc-bczRLJ"
cause This is a React hydration mismatch error, often caused by inconsistent CSS class generation between server and client during Server-Side Rendering (SSR) or Static Site Generation (SSG).fixVerify that your SSR setup is correctly rehydrating the styled-components stylesheet. This often involves using `ServerStyleSheet` on the server and `StyleSheetManager` with `enableCssr` on the client, ensuring the same `nonce` is used if applicable. -
Styled component's 'as' prop is not working as expected, component styles are not applied.
cause The `as` prop allows dynamic tag rendering. If styles aren't applied, it might be due to incorrect usage, an incompatible component passed, or a conflict with other styling solutions.fixEnsure the component passed to the `as` prop is a valid React component or a string representing a DOM element. Also, verify that no conflicting styles or global resets are overriding styled-components' injected CSS specificity.
Warnings
- breaking Styled Components v6 dropped support for Internet Explorer 11. The build target is now ES2015, and internal dependencies like `@emotion/unitless` have been removed, impacting older browser compatibility.
- breaking The `.attrs()` callback now receives an immutable snapshot of props, preventing unintended mutations that could affect subsequent `attrs` processing. Additionally, props supplied via `.attrs()` are automatically made optional on the resulting component's types.
- gotcha In React Native environments, older versions might crash due to `document` references in the native build. Additionally, CSS syntax errors are now gracefully handled instead of causing crashes.
- gotcha Loading the CommonJS build in Node.js could result in a 'React is not defined' ReferenceError, especially around versions 6.3.10/6.3.11.
- gotcha Using `createGlobalStyle` with React StrictMode or in React Server Components (RSC) environments might lead to styles disappearing or incorrect behavior due to improper cleanup and style injection. Static global styles are now injected once.
- gotcha CSS block comments containing `//` (e.g., in URLs) or `url()` CSS function values with unquoted URLs containing `//` (like `https://example.com`) could be incorrectly stripped or lead to syntax errors due to misinterpretation as JavaScript line comments.
Install
-
npm install styled-components -
yarn add styled-components -
pnpm add styled-components
Imports
- styled
import { styled } from 'styled-components'import styled from 'styled-components'
- css
import css from 'styled-components/macro'
import { css } from 'styled-components' - createGlobalStyle
import { GlobalStyle } from 'styled-components'import { createGlobalStyle } from 'styled-components' - ThemeProvider
const ThemeProvider = require('styled-components').ThemeProviderimport { ThemeProvider } from 'styled-components'
Quickstart
import styled, { ThemeProvider, createGlobalStyle, css } from 'styled-components';
const GlobalStyle = createGlobalStyle`
body {
margin: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
`;
const Button = styled.button`
background: ${props => props.primary ? props.theme.colors.primary : 'white'};
color: ${props => props.primary ? 'white' : props.theme.colors.primary};
font-size: 1em;
margin: 1em;
padding: 0.25em 1em;
border: 2px solid ${props => props.theme.colors.primary};
border-radius: 3px;
${props => props.size === 'large' && css`
font-size: 1.2em;
padding: 0.5em 1.5em;
`}
`;
const theme = {
colors: {
primary: '#007bff',
secondary: '#6c757d'
}
};
function App() {
return (
<ThemeProvider theme={theme}>
<GlobalStyle />
<div>
<Button>Normal Button</Button>
<Button primary>Primary Button</Button>
<Button primary size="large">Large Primary Button</Button>
</div>
</ThemeProvider>
);
}
export default App;