Styletron React Bindings
Styletron-react (current version 6.1.1) provides React bindings for Styletron, a universal, high-performance CSS-in-JS engine. It streamlines styling in React applications by offering an API inspired by `styled-components` but primarily uses JavaScript objects for styles instead of template strings. Key differentiators include its "atomic CSS" approach, which generates highly optimized, declaration-level deduplicated CSS, minimizing bundle size and improving critical rendering path performance for server-rendered pages. It also boasts efficient client-side style injection with hyper-granular memoization and fast cache hydration. Styletron aims to eliminate global namespace concerns, simplify dependencies, and handle dead code elimination and minification effectively, requiring no extra tooling beyond npm. The library is actively maintained, with a typical release cycle of around 34 days, as seen in broader Styletron comparisons. It supports both traditional styled components and a `useStyletron` hook for flexible, performant styling.
Common errors
-
Error: A Styletron styled component was rendered, but no Styletron engine instance was provided in React context. Did you forget to provide a Styletron engine instance to React context via using the Styletron provider component?
cause The `StyletronProvider` component (or its alias) was not rendered at a high enough level in the component tree, or multiple instances of `styletron-react` exist in `node_modules` causing a context mismatch.fixEnsure your entire React application is wrapped by `StyletronProvider` and that you pass a valid `styletron-engine` instance (e.g., `new Client()`) to its `value` prop. Verify there are no duplicate `styletron-react` packages in your dependency tree. -
Warning: React does not recognize the $somePropName prop on a DOM element.
cause A prop prefixed with `$` was inadvertently passed to a native DOM element (like `<div>` or `<span>`) instead of being consumed by the `styled` component or `useStyletron` hook.fixThis warning indicates that Styletron's prop filtering is working as intended. Ensure that `$somePropName` is only used for styling or internal logic within your styled component or hook, and is not passed explicitly to the underlying DOM element. If you see this, it implies React did not recognize the prop you intended to filter.
Warnings
- breaking Styletron-react v5.0.0 and later require React version 16.8.0 or higher due to the introduction of React Hooks, specifically the `useStyletron` hook. Ensure your React dependency meets this minimum requirement.
- breaking In v5.0.0, the original `withStyle` functionality was replaced by `withStyleDeep`. While `withStyle` is temporarily aliased to `withStyleDeep` for backward compatibility, its behavior changed to deep merging styles. Additionally, the `$ref` prop was removed in favor of standard React `ref` forwarding.
- gotcha Styletron-react filters out props prefixed with `$` (e.g., `$isActive`, `$theme`) so they are not passed down to the underlying DOM element, preventing React warnings about unknown DOM attributes. Remember to prefix your custom props if they are used solely for styling or internal logic and should not appear on the DOM element.
- gotcha Avoid mixing shorthand and longhand CSS properties within the same style object for a single `styled` component or `useStyletron` call. Due to Styletron's atomic CSS approach and JavaScript object property iteration order, this can lead to non-deterministic styling outcomes.
Install
-
npm install styletron-react -
yarn add styletron-react -
pnpm add styletron-react
Imports
- styled
const styled = require('styletron-react').styled;import { styled } from 'styletron-react'; - useStyletron
const useStyletron = require('styletron-react').useStyletron;import { useStyletron } from 'styletron-react'; - StyletronProvider
import StyletronProvider from 'styletron-react/provider';
import { StyletronProvider } from 'styletron-react'; - DebugEngine
import { DebugEngine } from 'styletron-react';
Quickstart
import * as React from 'react';
import { render } from 'react-dom';
import { Provider as StyletronProvider, styled, useStyletron, DebugEngine } from 'styletron-react';
import { Client as Styletron } from 'styletron-engine-atomic';
// 1. Create a client engine instance
const engine = new Styletron();
// 2. Create a debug engine instance for development
const debug = process.env.NODE_ENV === 'production' ? null : new DebugEngine();
// 3. Create a styled component
const RedText = styled('span', {
color: 'red',
fontSize: '24px',
fontWeight: 'bold',
':hover': {
color: 'darkred'
}
});
// 4. Create a functional component using the hook
function MyHookComponent() {
const [css] = useStyletron();
const dynamicColor = Math.random() > 0.5 ? 'blue' : 'green';
return (
<p className={css({
color: dynamicColor,
fontSize: '18px',
padding: '8px',
backgroundColor: 'lightgray',
borderRadius: '4px'
})}>
This text is styled with the useStyletron hook and has a {dynamicColor} color.
</p>
);
}
// 5. Wrap your application with the StyletronProvider
function App() {
return (
<StyletronProvider value={engine} debug={debug}>
<div>
<h1>Welcome to Styletron!</h1>
<RedText>This is a styled component.</RedText>
<MyHookComponent />
<p>
Styletron provides high-performance, atomic CSS-in-JS solutions,
optimizing CSS delivery and bundle size by de-duplicating styles.
</p>
</div>
</StyletronProvider>
);
}
render(<App />, document.getElementById('root'));