Styletron React Bindings

6.1.1 · active · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates how to set up Styletron with `styletron-react`, including creating a client engine, using both the `styled` factory for component-based styling, and the `useStyletron` hook for dynamic, inline-style-like class generation.

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'));

view raw JSON →